|
| 1 | +from typing import List |
| 2 | +from unittest import TestCase, main |
| 3 | + |
| 4 | + |
| 5 | +class Node: |
| 6 | + def __init__(self, key, data=None): |
| 7 | + self.key = key |
| 8 | + self.data = data |
| 9 | + self.children = {} |
| 10 | + |
| 11 | + |
| 12 | +class Trie: |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.root = Node(None) |
| 16 | + |
| 17 | + def insert(self, word: str) -> None: |
| 18 | + curr_node = self.root |
| 19 | + for char in word: |
| 20 | + if char not in curr_node.children: |
| 21 | + curr_node.children[char] = Node(char) |
| 22 | + |
| 23 | + curr_node = curr_node.children[char] |
| 24 | + |
| 25 | + curr_node.data = word |
| 26 | + |
| 27 | + def search(self, word: str) -> bool: |
| 28 | + curr_node = self.root |
| 29 | + for char in word: |
| 30 | + if char in curr_node.children: |
| 31 | + curr_node = curr_node.children[char] |
| 32 | + else: |
| 33 | + return False |
| 34 | + |
| 35 | + return curr_node.data == word |
| 36 | + |
| 37 | + def startsWith(self, prefix: str) -> bool: |
| 38 | + curr_node = self.root |
| 39 | + for char in prefix: |
| 40 | + if char in curr_node.children: |
| 41 | + curr_node = curr_node.children[char] |
| 42 | + else: |
| 43 | + return False |
| 44 | + else: |
| 45 | + return True |
| 46 | + |
| 47 | + |
| 48 | +class Solution: |
| 49 | + def wordBreak(self, s: str, wordDict: List[str]) -> bool: |
| 50 | + return self.solveWithTrieDFSMemo(s, wordDict) |
| 51 | + |
| 52 | + """ |
| 53 | + Runtime: 38 ms (Beats 70.76%) |
| 54 | + Time Complexity: O(n * L + s^2) |
| 55 | + - s의 길이를 S, wordDict의 길이를 L, word의 최대 길이를 W라 하면 |
| 56 | + - trie에 insert하는데 O(W * L), upper bound |
| 57 | + - DFS에서 curr_s의 모든 문자 조회에 최대 O(S), 접두사를 제거하는 curr_s.removeprefix에서 최대 O(S) |
| 58 | + > O(W * L) + O(S) * O(S) = O(W * L + S^2) upper bound |
| 59 | +
|
| 60 | + Memory: 17.70 MB (Beats 5.25%) |
| 61 | + Space Complexity: |
| 62 | + - trie 생성 및 갱신에 O(W * L) |
| 63 | + - visited는 최악의 경우 s의 모든 부분 문자열을 저장하므로 O(S^2) upper bound |
| 64 | + > O(W * L) + O(S^2) = O(W * L + S^2) upper bound |
| 65 | + """ |
| 66 | + def solveWithTrieDFSMemo(self, s: str, wordDict: List[str]) -> bool: |
| 67 | + trie = Trie() |
| 68 | + for word in wordDict: |
| 69 | + trie.insert(word) |
| 70 | + |
| 71 | + stack = [s] |
| 72 | + visited = set() |
| 73 | + while stack: |
| 74 | + curr_s = stack.pop() |
| 75 | + if curr_s in visited: |
| 76 | + continue |
| 77 | + |
| 78 | + curr_node = trie.root |
| 79 | + for char in curr_s: |
| 80 | + if char in curr_node.children: |
| 81 | + curr_node = curr_node.children[char] |
| 82 | + if curr_node.data is not None: |
| 83 | + post_s = curr_s.removeprefix(curr_node.data) |
| 84 | + if not post_s: |
| 85 | + return True |
| 86 | + else: |
| 87 | + stack.append(post_s) |
| 88 | + else: |
| 89 | + visited.add(curr_s) |
| 90 | + break |
| 91 | + |
| 92 | + return False |
| 93 | + |
| 94 | + |
| 95 | +class _LeetCodeTestCases(TestCase): |
| 96 | + def test_1(self): |
| 97 | + s = "leetcode" |
| 98 | + wordDict = ["leet","code"] |
| 99 | + output = True |
| 100 | + self.assertEqual(Solution.wordBreak(Solution(), s, wordDict), output) |
| 101 | + |
| 102 | + def test_2(self): |
| 103 | + s = "applepenapple" |
| 104 | + wordDict = ["apple","pen"] |
| 105 | + output = True |
| 106 | + self.assertEqual(Solution.wordBreak(Solution(), s, wordDict), output) |
| 107 | + |
| 108 | + def test_3(self): |
| 109 | + s = "catsandog" |
| 110 | + wordDict = ["cats","dog","sand","and","cat"] |
| 111 | + output = False |
| 112 | + self.assertEqual(Solution.wordBreak(Solution(), s, wordDict), output) |
| 113 | + |
| 114 | + def test_4(self): |
| 115 | + s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" |
| 116 | + wordDict = ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"] |
| 117 | + output = False |
| 118 | + self.assertEqual(Solution.wordBreak(Solution(), s, wordDict), output) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + main() |
0 commit comments