|
36 | 36 | <strong>输出:</strong> false
|
37 | 37 | </pre>
|
38 | 38 |
|
39 |
| - |
40 | 39 | ## 解法
|
41 | 40 |
|
42 | 41 | <!-- 这里可写通用的实现逻辑 -->
|
43 | 42 |
|
| 43 | +动态规划法。 |
| 44 | + |
| 45 | +`dp[i]` 表示前 i 个字符组成的字符串 `s[0...i-1]` 能否拆分成若干个字典中出现的单词。 |
| 46 | + |
44 | 47 | <!-- tabs:start -->
|
45 | 48 |
|
46 | 49 | ### **Python3**
|
47 | 50 |
|
48 | 51 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
49 | 52 |
|
50 | 53 | ```python
|
51 |
| - |
| 54 | +class Solution: |
| 55 | + def wordBreak(self, s: str, wordDict: List[str]) -> bool: |
| 56 | + words = set(wordDict) |
| 57 | + n = len(s) |
| 58 | + dp = [False] * (n + 1) |
| 59 | + dp[0] = True |
| 60 | + for i in range(1, n + 1): |
| 61 | + for j in range(i): |
| 62 | + if dp[j] and s[j:i] in words: |
| 63 | + dp[i] = True |
| 64 | + break |
| 65 | + return dp[n] |
52 | 66 | ```
|
53 | 67 |
|
54 | 68 | ### **Java**
|
55 | 69 |
|
56 | 70 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 71 |
|
58 | 72 | ```java
|
| 73 | +class Solution { |
| 74 | + public boolean wordBreak(String s, List<String> wordDict) { |
| 75 | + Set<String> words = new HashSet<>(wordDict); |
| 76 | + int n = s.length(); |
| 77 | + boolean[] dp = new boolean[n + 1]; |
| 78 | + dp[0] = true; |
| 79 | + for (int i = 1; i <= n; ++i) { |
| 80 | + for (int j = 0; j < i; ++j) { |
| 81 | + if (dp[j] && words.contains(s.substring(j, i))) { |
| 82 | + dp[i] = true; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return dp[n]; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### **C++** |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + bool wordBreak(string s, vector<string>& wordDict) { |
| 98 | + unordered_set<string> words; |
| 99 | + for (auto word : wordDict) { |
| 100 | + words.insert(word); |
| 101 | + } |
| 102 | + int n = s.size(); |
| 103 | + vector<bool> dp(n + 1, false); |
| 104 | + dp[0] = true; |
| 105 | + for (int i = 1; i <= n; ++i) { |
| 106 | + for (int j = 0; j < i; ++j) { |
| 107 | + if (dp[j] && words.find(s.substr(j, i - j)) != words.end()) { |
| 108 | + dp[i] = true; |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return dp[n]; |
| 114 | + } |
| 115 | +}; |
| 116 | +``` |
| 117 | +
|
| 118 | +### **Go** |
| 119 | +
|
| 120 | +```go |
| 121 | +func wordBreak(s string, wordDict []string) bool { |
| 122 | + words := make(map[string]bool) |
| 123 | + for _, word := range wordDict { |
| 124 | + words[word] = true |
| 125 | + } |
| 126 | + n := len(s) |
| 127 | + dp := make([]bool, n+1) |
| 128 | + dp[0] = true |
| 129 | + for i := 1; i <= n; i++ { |
| 130 | + for j := 0; j < i; j++ { |
| 131 | + if dp[j] && words[s[j:i]] { |
| 132 | + dp[i] = true |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + return dp[n] |
| 138 | +} |
| 139 | +``` |
59 | 140 |
|
| 141 | +### **C#** |
| 142 | + |
| 143 | +```cs |
| 144 | +public class Solution { |
| 145 | + public bool WordBreak(string s, IList<string> wordDict) { |
| 146 | + var words = new HashSet<string>(wordDict); |
| 147 | + int n = s.Length; |
| 148 | + var dp = new bool[n + 1]; |
| 149 | + dp[0] = true; |
| 150 | + for (int i = 1; i <= n; ++i) |
| 151 | + { |
| 152 | + for (int j = 0; j < i; ++j) |
| 153 | + { |
| 154 | + if (dp[j] && words.Contains(s.Substring(j, i - j))) |
| 155 | + { |
| 156 | + dp[i] = true; |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + return dp[n]; |
| 162 | + } |
| 163 | +} |
60 | 164 | ```
|
61 | 165 |
|
62 | 166 | ### **...**
|
|
0 commit comments