This file contains hidden or 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
#v1. DFS + Memoization,需要加大recursion limit | |
import sys | |
sys.setrecursionlimit(100000000) | |
class Solution: | |
def wordBreak(self, s, dict): | |
memo = {} # index: false | |
max_len = -1 | |
for word in dict: | |
max_len = max(max_len, len(word)) | |
ans = self.dfs(s, 0, dict, memo, max_len) |