[LeetCode][Python3] 140. Word Break II
                2019. 4. 25. 01:53 |
                
                    프로그래밍/LeetCode
                
            
            
            
        Problem :
https://leetcode.com/problems/word-break-ii/
My Solution :
class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
        def dfs(s):
            if s in memo:
                return memo[s]
            res = []
            for word in wordDict:
                if s.startswith(word):
                    cases = dfs(s[len(word):])
                    for case in cases:
                        res.append([word] + case)
            memo[s] = res
            return res
        memo = {'': [[]]}
        dfs(s)
        return [' '.join(case) for case in memo[s]]
Comment :
Memoization이 핵심이다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
| [LeetCode][Python3] 1079. Letter Tile Possibilities (0) | 2019.08.16 | 
|---|---|
| [LeetCode][Python3] 765. Couples Holding Hands (0) | 2019.08.15 | 
| [LeetCode][Python3] 854. K-Similar Strings (0) | 2019.08.15 | 
| [LeetCode][Python3] 5. Longest Palindromic Substring (0) | 2019.04.30 | 
| [LeetCode][Python3] 50. Pow(x, n) (0) | 2019.04.24 | 
| [LeetCode][Python3] 212. Word Search II (0) | 2019.04.24 | 
| [LeetCode][Python3] 41. First Missing Positive (0) | 2019.04.23 | 
| [LeetCode][Python3] 152. Maximum Product Subarray (0) | 2019.04.22 | 
최근에 달린 댓글 최근에 달린 댓글