-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
22 lines (20 loc) · 809 Bytes
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
# Cache booleans for places in the string that matches a word
# Put a True at beginning to signify start
dp = [True] + [False]*len(s)
for i in range(len(s)):
for word in wordDict:
# If current substring matches word and last word was valid then set True
if word == s[i-len(word)+1:i+1] and dp[i-len(word)+1]:
dp[i+1] = True
return dp[-1]
#-------------------------------------------------------------------------------
# Testing