Merge pull request #2019 from ZerenZhang2022/patch-27

Update 0139.单词拆分.md
This commit is contained in:
程序员Carl
2023-04-24 09:34:45 +08:00
committed by GitHub

View File

@ -351,7 +351,17 @@ class Solution:
dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j])
return dp[len(s)]
```
```python
class Solution: # 和视频中写法一致和最上面C++写法一致)
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
dp = [False]*(len(s)+1)
dp[0]=True
for j in range(1,len(s)+1):
for i in range(j):
word = s[i:j]
if word in wordDict and dp[i]: dp[j]=True
return dp[-1]
```