| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- shiba
- Python
- Python Code
- 30. Substring with Concatenation of All Words
- kaggle
- 715. Range Module
- LeetCode
- Python Implementation
- Generator
- iterator
- 밴픽
- 시바견
- Regular Expression
- Class
- Protocol
- data science
- attribute
- Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- DWG
- 파이썬
- Decorator
- 프로그래머스
- 컴퓨터의 구조
- 43. Multiply Strings
- t1
- 운영체제
- concurrency
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
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 |