aboutsummaryrefslogtreecommitdiff
path: root/word_break2.py
blob: 7c741c84b66a8ab86ed5cd0402bd0589ef9e4f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 140. Word Break II

"""
given a string 's' and a dictionary of strings 'wordDict', add spaces in 's'
to construct a sentence where each word is a valid dictionary word. return
all such possible sentences in any order. note that the same word in the
dictionary may be reused mutliple times in the segmentation.
"""


class Solution(object):
    def dfs(self, s, wordDict, memo):
        if s in memo:
            return memo[s]
        if not s:
            return []
        res = []
        for word in wordDict:
            if not s.startswith(word):
                continue
            if len(word) == len(s):
                res.append(word)
            else:
                res_rest = self.dfs(s[len(word) :], wordDict, memo)
                for item in res_rest:
                    item = word + " " + item
                    res.append(item)
        memo[s] = res
        return res

    def wordBreak(self, s, wordDict):
        """
        :type s: str
        :type wordDict: List[str]
        :rtype: List[str]
        """
        return self.dfs(s, wordDict, {})


if __name__ == "__main__":
    obj = Solution()
    print(obj.wordBreak(s="catsanddog", wordDict=["cat", "cats", "and", "sand", "dog"]))
    print(
        obj.wordBreak(
            s="pineapplepenapple",
            wordDict=["apple", "pen", "applepen", "pine", "pineapple"],
        )
    )
    print(obj.wordBreak(s="catsandog", wordDict=["cats", "dog", "sand", "and", "cat"]))