Easy Python Solution

  • 0
    class Solution(object):
        def wordBreak(self, s, wordDict):
            self.memo = {}
            self.words = wordDict
            return self.solve(s)
        def solve(self, s):
            if s not in self.memo:
                self.memo[s] = self._solve(s)
            return self.memo[s]
        def _solve(self, s):
            if not s:
                return True
            for w in self.words:
                if s.startswith(w) and self.solve(s[len(w):]):
                    return True
            return False

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.