| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- Generator
- Python Code
- 밴픽
- Substring with Concatenation of All Words
- LeetCode
- 컴퓨터의 구조
- Regular Expression
- 715. Range Module
- Decorator
- 315. Count of Smaller Numbers After Self
- 파이썬
- 109. Convert Sorted List to Binary Search Tree
- data science
- 30. Substring with Concatenation of All Words
- t1
- attribute
- kaggle
- Python
- 프로그래머스
- concurrency
- 시바견
- Class
- Python Implementation
- 43. Multiply Strings
- shiba
- 운영체제
- DWG
- Protocol
- Convert Sorted List to Binary Search Tree
- iterator
Archives
- Today
- Total
Scribbling
LeetCode: 745. Prefix and Suffix Search 본문
Using double trie structures for prefix and suffix.
Somewhat straightforward if one already knows about trie.
If not, refer to "LeetCode prob 208. Implement Trie (Prefix Tree)".
class WordFilter:
def __init__(self, words: List[str]):
wordDict = {}
for idx, word in enumerate(words):
wordDict[word] = idx
self.PrefixTree = {}
self.SuffixTree = {}
for word, idx in wordDict.items():
node = self.PrefixTree
for c in word:
if c not in node:
node[c] = {}
node = node[c]
if 'IdxList' not in node:
node['IdxList'] = []
node['IdxList'].append(idx)
node['#'] = '#'
node = self.SuffixTree
for c in word[::-1]:
if c not in node:
node[c] = {}
node = node[c]
if 'IdxList' not in node:
node['IdxList'] = []
node['IdxList'].append(idx)
node['#'] = '#'
def f(self, prefix: str, suffix: str) -> int:
node = self.PrefixTree
for c in prefix:
if c not in node:
return -1
node = node[c]
PrefixMatchList = node['IdxList']
node = self.SuffixTree
for c in suffix[::-1]:
if c not in node:
return -1
node = node[c]
SuffixMatchList = node['IdxList']
MatchList = list(set(PrefixMatchList) & set(SuffixMatchList))
return -1 if not MatchList else max(MatchList)
# Your WordFilter object will be instantiated and called as such:
# obj = WordFilter(words)
# param_1 = obj.f(prefix,suffix)'Computer Science > Coding Test' 카테고리의 다른 글
| LeetCode: 833. Find And Replace in String (0) | 2022.03.15 |
|---|---|
| LeetCode: 772. Basic Calculator III (0) | 2022.03.11 |
| LeetCode: 1834. Single-Threaded CPU (0) | 2022.03.08 |
| LeetCode: 407. Trapping Rain Water II (0) | 2022.03.08 |
| LeetCode: 1610. Maximum Number of Visible Points (0) | 2022.03.07 |