Leetcode 139 Solution

This article provides solution to leetcode question 139 (word-break)

https://leetcode.com/problems/word-break

Solution

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        dp = [False] * (len(s) + 1)
        dp[0] = True

        for i in range(len(s)):
            if dp[i] == False:
                continue

            for word in wordDict:
                if word == s[i:len(word) + i]:
                    dp[len(word) + i] = True

        return dp[len(s)]