diff --git a/src/my_project/interviews/google_top_exercises/round_1/13_word_break.py b/src/my_project/interviews/google_top_exercises/round_1/13_word_break.py new file mode 100644 index 00000000..dbbf6b3b --- /dev/null +++ b/src/my_project/interviews/google_top_exercises/round_1/13_word_break.py @@ -0,0 +1,21 @@ +from typing import List + + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + words = set(wordDict) + n = len(s) + + # dp[i] is True if s[:i] can be segmented into dictionary words. + # Empty prefix is trivially segmentable. + dp = [False] * (n + 1) + dp[0] = True + + for i in range(1, n + 1): + # Try every split point j: s[:j] segmentable and s[j:i] a word. + for j in range(i): + if dp[j] and s[j:i] in words: + dp[i] = True + break + + return dp[n] \ No newline at end of file