Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f109e78
commit 5f3e1ce
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Solution: | ||
# @param s, a string | ||
# @param dict, a set of string | ||
# @return a boolean | ||
def wordBreak(self, s, dict): | ||
if len(s) == 0: | ||
return False | ||
ss = len(s) | ||
mp = [[False for i in range(ss)] for j in range(ss)] | ||
|
||
for i in range(ss): | ||
for j in range(i, ss): | ||
if s[i:j+1] in dict: | ||
mp[i][j] = True | ||
|
||
|
||
for i in range(ss): | ||
for j in range(i, ss): | ||
for k in range(i, j): | ||
if mp[i][j]==False: | ||
mp[i][j] = mp[i][k] and mp[k+1][j] | ||
|
||
return mp[0][ss-1] | ||
|