C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class StreamChecker {
public:
StreamChecker(vector<string>& words) {
}
bool query(char letter) {
}
};
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker* obj = new StreamChecker(words);
* bool param_1 = obj->query(letter);
*/
运行代码
提交
golang 解法, 执行用时: 164 ms, 内存消耗: 29.4 MB, 提交时间: 2023-03-24 16:47:46
type Trie struct {
children [26]*Trie
isEnd bool
}
func newTrie() Trie {
return Trie{}
}
func (this *Trie) Insert(word string) {
node := this
for i := len(word) - 1; i >= 0; i-- {
idx := word[i] - 'a'
if node.children[idx] == nil {
node.children[idx] = &Trie{}
}
node = node.children[idx]
}
node.isEnd = true
}
func (this *Trie) Search(word []byte) bool {
node := this
for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 {
idx := word[i] - 'a'
if node.children[idx] == nil {
return false
}
node = node.children[idx]
if node.isEnd {
return true
}
}
return false
}
type StreamChecker struct {
trie Trie
s []byte
}
func Constructor(words []string) StreamChecker {
trie := newTrie()
for _, w := range words {
trie.Insert(w)
}
return StreamChecker{trie, []byte{}}
}
func (this *StreamChecker) Query(letter byte) bool {
this.s = append(this.s, letter)
return this.trie.Search(this.s)
}
/**
* Your StreamChecker object will be instantiated and called as such:
* obj := Constructor(words);
* param_1 := obj.Query(letter);
*/
python3 解法, 执行用时: 620 ms, 内存消耗: 50.2 MB, 提交时间: 2023-03-24 16:47:06
# 前缀树
class Trie:
def __init__(self):
self.children = [None] * 26
self.is_end = False
def insert(self, w: str):
node = self
for c in w[::-1]:
idx = ord(c) - ord('a')
if node.children[idx] is None:
node.children[idx] = Trie()
node = node.children[idx]
node.is_end = True
def search(self, w: List[str]) -> bool:
node = self
for c in w[::-1]:
idx = ord(c) - ord('a')
if node.children[idx] is None:
return False
node = node.children[idx]
if node.is_end:
return True
return False
class StreamChecker:
def __init__(self, words: List[str]):
self.trie = Trie()
self.cs = []
self.limit = 201
for w in words:
self.trie.insert(w)
def query(self, letter: str) -> bool:
self.cs.append(letter)
return self.trie.search(self.cs[-self.limit:])
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)
javascript 解法, 执行用时: 400 ms, 内存消耗: 71.1 MB, 提交时间: 2023-03-24 16:46:39
/**
* @param {string[]} words
*/
var StreamChecker = function(words) {
const root = new TrieNode();
for (const word of words) {
let cur = root;
for (let i = 0; i < word.length; i++) {
const index = word[i].charCodeAt() - 'a'.charCodeAt();
if (cur.getChild(index) === 0) {
cur.setChild(index, new TrieNode());
}
cur = cur.getChild(index);
}
cur.setIsEnd(true);
}
root.setFail(root);
const q = [];
for (let i = 0; i < 26; i++) {
if(root.getChild(i) != 0) {
root.getChild(i).setFail(root);
q.push(root.getChild(i));
} else {
root.setChild(i, root);
}
}
while (q.length) {
const node = q.shift();
node.setIsEnd(node.getIsEnd() || node.getFail().getIsEnd());
for (let i = 0; i < 26; i++) {
if(node.getChild(i) != 0) {
node.getChild(i).setFail(node.getFail().getChild(i));
q.push(node.getChild(i));
} else {
node.setChild(i, node.getFail().getChild(i));
}
}
}
this.temp = root;
};
/**
* @param {character} letter
* @return {boolean}
*/
StreamChecker.prototype.query = function(letter) {
this.temp = this.temp.getChild(letter.charCodeAt() - 'a'.charCodeAt());
return this.temp.getIsEnd();
};
class TrieNode {
constructor() {
this.children = new Array(26).fill(0);
this.isEnd = false;
this.fail;
}
getChild(index) {
return this.children[index];
}
setChild(index, node) {
this.children[index] = node;
}
getIsEnd() {
return this.isEnd;
}
setIsEnd(b) {
this.isEnd = b;
}
getFail() {
return this.fail;
}
setFail(node) {
this.fail = node;
}
}
/**
* Your StreamChecker object will be instantiated and called as such:
* var obj = new StreamChecker(words)
* var param_1 = obj.query(letter)
*/
java 解法, 执行用时: 63 ms, 内存消耗: 70.9 MB, 提交时间: 2023-03-24 16:45:52
class StreamChecker {
TrieNode root;
TrieNode temp;
public StreamChecker(String[] words) {
root = new TrieNode();
for (String word : words) {
TrieNode cur = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (cur.getChild(index) == null) {
cur.setChild(index, new TrieNode());
}
cur = cur.getChild(index);
}
cur.setIsEnd(true);
}
root.setFail(root);
Queue<TrieNode> q = new LinkedList<>();
for (int i = 0; i < 26; i++) {
if(root.getChild(i) != null) {
root.getChild(i).setFail(root);
q.add(root.getChild(i));
} else {
root.setChild(i, root);
}
}
while (!q.isEmpty()) {
TrieNode node = q.poll();
node.setIsEnd(node.getIsEnd() || node.getFail().getIsEnd());
for (int i = 0; i < 26; i++) {
if(node.getChild(i) != null) {
node.getChild(i).setFail(node.getFail().getChild(i));
q.offer(node.getChild(i));
} else {
node.setChild(i, node.getFail().getChild(i));
}
}
}
temp = root;
}
public boolean query(char letter) {
temp = temp.getChild(letter - 'a');
return temp.getIsEnd();
}
}
class TrieNode {
TrieNode[] children;
boolean isEnd;
TrieNode fail;
public TrieNode() {
children = new TrieNode[26];
}
public TrieNode getChild(int index) {
return children[index];
}
public void setChild(int index, TrieNode node) {
children[index] = node;
}
public boolean getIsEnd() {
return isEnd;
}
public void setIsEnd(boolean b) {
isEnd = b;
}
public TrieNode getFail() {
return fail;
}
public void setFail(TrieNode node) {
fail = node;
}
}
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* boolean param_1 = obj.query(letter);
*/
python3 解法, 执行用时: 556 ms, 内存消耗: 50.4 MB, 提交时间: 2023-03-24 16:45:32
# AC自动机
class StreamChecker:
def __init__(self, words: List[str]):
self.root = TrieNode()
for word in words:
cur = self.root
for char in word:
idx = ord(char) - ord('a')
if not cur.children[idx]:
cur.children[idx] = TrieNode()
cur = cur.children[idx]
cur.isEnd = True
self.root.fail = self.root
q = deque()
for i in range(26):
if self.root.children[i]:
self.root.children[i].fail = self.root
q.append(self.root.children[i])
else:
self.root.children[i] = self.root
while q:
node = q.popleft()
node.isEnd = node.isEnd or node.fail.isEnd
for i in range(26):
if node.children[i]:
node.children[i].fail = node.fail.children[i]
q.append(node.children[i])
else:
node.children[i] = node.fail.children[i]
self.temp = self.root
def query(self, letter: str) -> bool:
self.temp = self.temp.children[ord(letter) - ord('a')]
return self.temp.isEnd
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEnd = False
self.fail = None
# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)