1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// @Title: 单词拆分 (Word Break)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 0 ms
// @Memory: 2.2 MB
func wordBreak(s string, wordDict []string) bool {
    mp := make(map[string]bool)
    for _, word := range wordDict {
        mp[word] = true
    }
    
    dp := make([]bool, len(s)+1)
    dp[0] = true
    
    // dp
    for i :=1;i<=len(s);i++ {
        for j:=0;j<=i;j++ {
            if dp[j] && mp[s[j:i]] {
                dp[i] = true
                break
            }
            
        }
    }
    
    return dp[len(s)]
}