aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/sangeet-kar/python/ch-2.py
blob: a11a70ab8b6ee16f9a38a27077314ae5cf14ba5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python

def word_break(string, words):
    ans = []
    recursive_helper(string, words, [], ans) 
    return ans 

def recursive_helper(string, words, accum, ans):
    if not string:
        ans.append(accum)
        return
    for word in words:
        if string.startswith(word): 
            recursive_helper(string[len(word):], words, accum + [word], ans)


print(word_break("perlweeklychallenge", ["weekly", "challenge", "perl"]))
print(word_break("perlandraku", ["python", "ruby", "haskell"]))

# multiple solutions
print(word_break("perlandraku", ["perl", "perland", "raku", "and"]))