208 https://leetcode.com/problems/implement-trie-prefix-tree/
https://qkqhxla1.tistory.com/1081?category=685988 에서 만든 코드를 갖다 쓴다.
class Trie(object): def __init__(self): self.trie={} def insert(self, word): trie=self.trie for c in word: if c not in trie: trie[c]={} trie=trie[c] trie['#']='#' def search(self, word): stack = deque([[word, self.trie]]) while stack: word, trie = stack.pop() if not word: if '#' in trie: return True continue c = word[0] if c in trie: stack.append([word[1:], trie[c]]) return False def startsWith(self, prefix): stack = deque([[prefix, self.trie]]) while stack: word, trie = stack.pop() if not word: return True c = word[0] if c in trie: stack.append([word[1:], trie[c]]) return False
648 https://leetcode.com/problems/replace-words/
trie를 만들고 풀어준다.
from collections import deque class Solution(object): def replaceWords(self, wordlist, sentence): def make_trie(wordlist): trie = {} for word in wordlist: t = trie for w in word: if w not in t: t[w] = {} t = t[w] t['#'] = '#' return trie def search(word, trie): stack = deque([[word, '', 0, trie]]) ret_word = ['', 99999] while stack: word, ret, _len, trie = stack.pop() if '#' in trie and _len < ret_word[1]: ret_word = [ret, _len] if not word: continue if word[0] in trie: stack.append([word[1:], ret + word[0], _len + 1, trie[word[0]]]) return ret_word[0] trie = make_trie(wordlist) ret = [] for word in sentence.split(): result = search(word, trie) if result: ret.append(result) else: ret.append(word) return ' '.join(ret)
676 https://leetcode.com/problems/implement-magic-dictionary/
이것도 트라이를 만들고 조금씩 변형해서 푼다.
from collections import deque class MagicDictionary(object): def __init__(self): self.trie = {} def buildDict(self, d): for word in d: t = self.trie for w in word: if w not in t: t[w] = {} t = t[w] t['#'] = '#' def search(self, word): stack = deque([[word, True, self.trie]]) # word, 변형할수 있는지여부, trie while stack: word, flag, trie = stack.pop() if not word: if '#' in trie and not flag: return True continue if word[0] in trie: stack.append([word[1:], flag, trie[word[0]]]) if flag: # 아직 한개의 단어를 바꿀수 있으면. for key in trie: if key != '#' and word[0] != key: stack.append([word[1:], not flag, trie[key]]) return False
'algorithm > problem solving' 카테고리의 다른 글
leetcode 647(팰린드롬), 516(팰린드롬), 1143(팰린드롬, lcs), 583 (0) | 2020.06.13 |
---|---|
leetcode 24(구현, linked_list), 572(트리), 508(트리) (0) | 2020.06.10 |
leetcode 863(트리), 430(linked list, stack), 114, 211(구현, 정규식) (0) | 2020.06.06 |
leetcode 97(dfs), 767(구현, 힙), 991(구현), 650(dp) (0) | 2020.06.02 |
leetcode 79(dfs), 209(two pointer), 718(dp), 9(구현), 70(dp) (0) | 2020.05.29 |