diff --git a/word-break/prograsshopper.py b/word-break/prograsshopper.py new file mode 100644 index 000000000..e0c4ab3a8 --- /dev/null +++ b/word-break/prograsshopper.py @@ -0,0 +1,15 @@ +# Time complexity: O(N∗N∗M) +# Space complexity: O(N) +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [False] * (len(s) + 1) + dp[0] = True + wordSet = set(wordDict) + + for i in range(1, len(s) + 1): + for j in range(0, i): + if dp[j]: + if s[j:i] in wordSet: + dp[i] = True + break + return dp[len(s)]