|
| 1 | +''' |
| 2 | +solution1: dfs + @cache |
| 3 | +''' |
| 4 | +from typing import List |
| 5 | +from functools import cache |
| 6 | + |
| 7 | +class Solution: |
| 8 | + def wordBreak(self, s: str, wordDict: List[str]) -> bool: |
| 9 | + size = len(s) |
| 10 | + word_set = set(wordDict) |
| 11 | + |
| 12 | + @cache |
| 13 | + def dfs(start: int) -> bool: |
| 14 | + if start == size: |
| 15 | + return True |
| 16 | + |
| 17 | + for end in range(start + 1, size + 1): |
| 18 | + word = s[start:end] # O(N) |
| 19 | + |
| 20 | + if word in word_set and dfs(end): |
| 21 | + return True |
| 22 | + |
| 23 | + return False |
| 24 | + |
| 25 | + return dfs(0) |
| 26 | + |
| 27 | +''' |
| 28 | +solution2: dfs + memo |
| 29 | +''' |
| 30 | +class Solution: |
| 31 | + def wordBreak(self, s: str, wordDict: list[str]) -> bool: |
| 32 | + size = len(s) |
| 33 | + word_set = set(wordDict) |
| 34 | + failed: set[int] = set() # 탐색에 실패한 인덱스 기록 |
| 35 | + |
| 36 | + def dfs(start: int) -> bool: |
| 37 | + if start == size: |
| 38 | + return True |
| 39 | + |
| 40 | + if start in failed: |
| 41 | + return False |
| 42 | + |
| 43 | + for end in range(start + 1, size + 1): |
| 44 | + word = s[start:end] |
| 45 | + |
| 46 | + if word in word_set and dfs(end): |
| 47 | + return True |
| 48 | + |
| 49 | + failed.add(start) |
| 50 | + return False |
| 51 | + |
| 52 | + return dfs(0) |
| 53 | + |
| 54 | +''' |
| 55 | +solution3: bfs |
| 56 | +''' |
| 57 | +from collections import deque |
| 58 | + |
| 59 | +class Solution: |
| 60 | + def wordBreak(self, s: str, wordDict: list[str]) -> bool: |
| 61 | + size = len(s) |
| 62 | + word_set = set(wordDict) |
| 63 | + queue = deque([0]) |
| 64 | + visited = set([0]) |
| 65 | + |
| 66 | + while queue: |
| 67 | + start = queue.popleft() |
| 68 | + |
| 69 | + for end in range(start + 1, size + 1): |
| 70 | + word = s[start:end] |
| 71 | + |
| 72 | + if end not in visited and word in word_set: |
| 73 | + if end == size: |
| 74 | + return True |
| 75 | + |
| 76 | + queue.append(end) |
| 77 | + visited.add(end) |
| 78 | + |
| 79 | + return False |
| 80 | + |
| 81 | +''' |
| 82 | +solution4: dp |
| 83 | +''' |
| 84 | +class Solution: |
| 85 | + def wordBreak(self, s: str, wordDict: list[str]) -> bool: |
| 86 | + size = len(s) |
| 87 | + word_set = set(wordDict) |
| 88 | + dp = [False] * (size + 1) |
| 89 | + dp[0] = True |
| 90 | + |
| 91 | + for end in range(1, size + 1): |
| 92 | + for start in range(end): |
| 93 | + word = s[start:end] |
| 94 | + |
| 95 | + if dp[start] and word in word_set: |
| 96 | + dp[end] = True |
| 97 | + break |
| 98 | + |
| 99 | + return dp[-1] |
| 100 | + |
| 101 | +''' |
| 102 | +solution5: trie 자료구조 활용 |
| 103 | +
|
| 104 | +Trie 자료구조로 노드를 만들어서 탐색한다 |
| 105 | +Examples3: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] |
| 106 | +
|
| 107 | +[Root] |
| 108 | + ├── 'c' ── 'a' ── 't'(leaf) ── 's'(leaf) |
| 109 | + ├── 'd' ── 'o' ── 'g'(leaf) |
| 110 | + ├── 's' ── 'a' ── 'n' ── 'd'(leaf) |
| 111 | + └── 'a' ── 'n' ── 'd'(leaf) |
| 112 | +''' |
| 113 | +class TrieNode: |
| 114 | + def __init__(self): |
| 115 | + self.children: dict[int, 'TrieNode'] = {} |
| 116 | + self.is_leaf = False |
| 117 | + |
| 118 | +class Solution: |
| 119 | + def wordBreak(self, s: str, wordDict: list[str]) -> bool: |
| 120 | + root = self._build_trie(wordDict) |
| 121 | + |
| 122 | + size = len(s) |
| 123 | + dp = [False] * (size + 1) |
| 124 | + dp[0] = True |
| 125 | + |
| 126 | + for start in range(size): |
| 127 | + if not dp[start]: |
| 128 | + continue |
| 129 | + |
| 130 | + node = root |
| 131 | + for end in range(start + 1, size + 1): |
| 132 | + char = s[end - 1] |
| 133 | + # trie에 없으면 바로 종료 |
| 134 | + if char not in node.children: |
| 135 | + break |
| 136 | + node = node.children[char] |
| 137 | + |
| 138 | + if node.is_leaf: |
| 139 | + dp[end] = True |
| 140 | + |
| 141 | + return dp[-1] |
| 142 | + |
| 143 | + # wordDict로 trie 자료구조 생성 |
| 144 | + def _build_trie(self, wordDict: list[str]) -> TrieNode: |
| 145 | + root = TrieNode() |
| 146 | + |
| 147 | + for word in wordDict: |
| 148 | + node = root |
| 149 | + |
| 150 | + for char in word: |
| 151 | + if char not in node.children: |
| 152 | + node.children[char] = TrieNode() |
| 153 | + node = node.children[char] |
| 154 | + node.is_leaf = True |
| 155 | + |
| 156 | + return root |
0 commit comments