-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamofCharacters.py
More file actions
46 lines (36 loc) · 1.08 KB
/
streamofCharacters.py
File metadata and controls
46 lines (36 loc) · 1.08 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/stream-of-characters/
# Author: Miao Zhang
# Date: 2021-04-05
class TrieNode:
def __init__(self):
self.TrieNode = {}
def insert(self, word):
cur = self.TrieNode
for w in word:
if w not in cur:
cur[w] = {}
cur = cur[w]
cur['#'] = 1
def search(self, word):
cur = self.TrieNode
for w in word:
if w not in cur:
return False
if '#' in cur[w]:
return True
cur = cur[w]
return False
class StreamChecker:
def __init__(self, words: List[str]):
self.trie = TrieNode()
self.stream = deque([])
for word in set(words):
self.trie.insert(word[::-1])
def query(self, letter: str) -> bool:
self.stream.appendleft(letter)
return self.trie.search(self.stream)
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)