forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreak String Into Words.py
More file actions
24 lines (21 loc) · 913 Bytes
/
Copy pathBreak String Into Words.py
File metadata and controls
24 lines (21 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
Break String Into Words
We convert the words into a set to be able to do constant time lookup.
Then we start from the start of the string, iterating over the string and storing the characters.
If we match a word in the set, we recursively retry accumulating from that point on and if it doesn't work we keep accumulating till the end of the word. If we exhaust our options there is no solution.
lru_cache ensures we don't recompute the outcomes for the same value of i, reducing this problem to O(n^2)
"""
from functools import lru_cache
class Solution:
def solve(self, words, s):
words=set(words)
@lru_cache(None)
def rec(i=0):
if i == len(s): return True
acc=""
for j in range(i, len(s)):
acc+=s[j]
if acc in words:
if rec(j+1): return True
return False
return rec()