From 2b62803f1cd3648cc1fe8466ba1fec9c0a4f1ba9 Mon Sep 17 00:00:00 2001 From: prograsshopper Date: Tue, 9 Dec 2025 17:58:37 +0900 Subject: [PATCH] week 5: Word Break --- word-break/prograsshopper.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 word-break/prograsshopper.py diff --git a/word-break/prograsshopper.py b/word-break/prograsshopper.py new file mode 100644 index 0000000000..e0c4ab3a83 --- /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)]