A combination of DFS and DP. Use DFS to generate strings, and use DP to judge if it is valid.
Solution:
# T:O(n^2) S:O(n)
class Solution:
# @param s, a string
# @param dict, a set of string
# @return a list of strings
def check(self, s, dict):
dp = [False for i in range(len(s)+1)]
dp[0] = True
for i in range(1, len(s)+1):
for k in range(0, i):
if dp[k] and s[k:i] in dict:
dp[i] = True
return dp[len(s)]
def dfs(self, s, dict, stringlist):
if self.check(s, dict):
if len(s) == 0: Solution.res.append(stringlist[1:])
for i in range(1, len(s)+1):
if s[:i] in dict:
self.dfs(s[i:], dict, stringlist+' '+s[:i])
def wordBreak(self, s, dict):
Solution.res = []
self.dfs(s, dict, '')
return Solution.res
Run Time: 84 ms
No comments:
Post a Comment