class Trie {
vector<string> words;
public:
void insert(string word) {
words.push_back(word);
}
bool search(string word) {
for (const string& w : words) {
if (w == word) return true;
}
return false;
}
bool startsWith(string prefix) {
for (const string& w : words) {
if (w.compare(0, prefix.size(), prefix) == 0) return true;
}
return false;
}
};
class Trie {
private List<String> words = new ArrayList<>();
public void insert(String word) {
words.add(word);
}
public boolean search(String word) {
for (String w : words) {
if (w.equals(word)) return true;
}
return false;
}
public boolean startsWith(String prefix) {
for (String w : words) {
if (w.startsWith(prefix)) return true;
}
return false;
}
}
class Trie:
def __init__(self):
self.words = []
def insert(self, word):
self.words.append(word)
def search(self, word):
for w in self.words:
if w == word: return True
return False
def starts_with(self, prefix):
for w in self.words:
if w.startswith(prefix): return True
return False
class Trie {
private words: string[] = [];
insert(word: string): void {
this.words.push(word);
}
search(word: string): boolean {
for (const w of this.words) {
if (w === word) return true;
}
return false;
}
startsWith(prefix: string): boolean {
for (const w of this.words) {
if (w.startsWith(prefix)) return true;
}
return false;
}
}
class Trie {
unordered_set<string> words;
unordered_set<string> prefixes;
public:
void insert(string word) {
words.insert(word);
for (int i = 1; i <= (int)word.size(); i++) {
prefixes.insert(word.substr(0, i));
}
}
bool search(string word) {
return words.count(word) > 0;
}
bool startsWith(string prefix) {
return prefixes.count(prefix) > 0;
}
};
class Trie {
private Set<String> words = new HashSet<>();
private Set<String> prefixes = new HashSet<>();
public void insert(String word) {
words.add(word);
for (int i = 1; i <= word.length(); i++) {
prefixes.add(word.substring(0, i));
}
}
public boolean search(String word) {
return words.contains(word);
}
public boolean startsWith(String prefix) {
return prefixes.contains(prefix);
}
}
class Trie:
def __init__(self):
self.words = set()
self.prefixes = set()
def insert(self, word):
self.words.add(word)
for i in range(1, len(word) + 1):
self.prefixes.add(word[:i])
def search(self, word):
return word in self.words
def starts_with(self, prefix):
return prefix in self.prefixes
class Trie {
private words = new Set<string>();
private prefixes = new Set<string>();
insert(word: string): void {
this.words.add(word);
for (let i = 1; i <= word.length; i++) {
this.prefixes.add(word.slice(0, i));
}
}
search(word: string): boolean {
return this.words.has(word);
}
startsWith(prefix: string): boolean {
return this.prefixes.has(prefix);
}
}
struct TrieNode {
unordered_map<char, TrieNode*> children;
bool isEnd = false;
};
class Trie {
TrieNode* root = new TrieNode();
TrieNode* walk(const string& s) {
TrieNode* node = root;
for (char ch : s) {
auto it = node->children.find(ch);
if (it == node->children.end()) return nullptr;
node = it->second;
}
return node;
}
public:
void insert(string word) {
TrieNode* node = root;
for (char ch : word) {
if (!node->children.count(ch)) node->children[ch] = new TrieNode();
node = node->children[ch];
}
node->isEnd = true;
}
bool search(string word) {
TrieNode* node = walk(word);
return node != nullptr && node->isEnd;
}
bool startsWith(string prefix) {
return walk(prefix) != nullptr;
}
};
class TrieNode {
Map<Character, TrieNode> children = new HashMap<>();
boolean isEnd = false;
}
class Trie {
private TrieNode root = new TrieNode();
public void insert(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
if (!node.children.containsKey(ch)) node.children.put(ch, new TrieNode());
node = node.children.get(ch);
}
node.isEnd = true;
}
private TrieNode walk(String s) {
TrieNode node = root;
for (char ch : s.toCharArray()) {
TrieNode next = node.children.get(ch);
if (next == null) return null;
node = next;
}
return node;
}
public boolean search(String word) {
TrieNode node = walk(word);
return node != null && node.isEnd;
}
public boolean startsWith(String prefix) {
return walk(prefix) != null;
}
}
class TrieNode:
def __init__(self):
self.children = {}
self.is_end = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
node = self.root
for ch in word:
if ch not in node.children: node.children[ch] = TrieNode()
node = node.children[ch]
node.is_end = True
def _walk(self, s):
node = self.root
for ch in s:
nxt = node.children.get(ch)
if nxt is None: return None
node = nxt
return node
def search(self, word):
node = self._walk(word)
return node is not None and node.is_end
def starts_with(self, prefix):
return self._walk(prefix) is not None
class TrieNode {
children = new Map<string, TrieNode>();
isEnd = false;
}
class Trie {
private root = new TrieNode();
insert(word: string): void {
let node = this.root;
for (const ch of word) {
if (!node.children.has(ch)) node.children.set(ch, new TrieNode());
node = node.children.get(ch)!;
}
node.isEnd = true;
}
private walk(s: string): TrieNode | null {
let node = this.root;
for (const ch of s) {
const next = node.children.get(ch);
if (!next) return null;
node = next;
}
return node;
}
search(word: string): boolean {
const node = this.walk(word);
return node !== null && node.isEnd;
}
startsWith(prefix: string): boolean {
return this.walk(prefix) !== null;
}
}
class Trie {
vector<int> next = vector<int>(26, 0);
vector<bool> end = {false};
int find(const string& s) {
int node = 0;
for (char ch : s) {
node = next[node * 26 + (ch - 'a')];
if (node == 0) return -1;
}
return node;
}
public:
void insert(string word) {
int node = 0;
for (char ch : word) {
int c = ch - 'a';
if (next[node * 26 + c] == 0) {
next[node * 26 + c] = end.size();
end.push_back(false);
next.insert(next.end(), 26, 0);
}
node = next[node * 26 + c];
}
end[node] = true;
}
bool search(string word) {
int node = find(word);
return node != -1 && end[node];
}
bool startsWith(string prefix) {
return find(prefix) != -1;
}
};
class Trie {
private List<Integer> next = new ArrayList<>(Collections.nCopies(26, 0));
private List<Boolean> end = new ArrayList<>(List.of(false));
public void insert(String word) {
int node = 0;
for (char ch : word.toCharArray()) {
int c = ch - 'a';
if (next.get(node * 26 + c) == 0) {
next.set(node * 26 + c, end.size());
end.add(false);
next.addAll(Collections.nCopies(26, 0));
}
node = next.get(node * 26 + c);
}
end.set(node, true);
}
private int find(String s) {
int node = 0;
for (char ch : s.toCharArray()) {
node = next.get(node * 26 + (ch - 'a'));
if (node == 0) return -1;
}
return node;
}
public boolean search(String word) {
int node = find(word);
return node != -1 && end.get(node);
}
public boolean startsWith(String prefix) {
return find(prefix) != -1;
}
}
class Trie:
def __init__(self):
self.next = [0] * 26
self.end = [False]
def insert(self, word):
node = 0
for ch in word:
c = ord(ch) - 97
if self.next[node * 26 + c] == 0:
self.next[node * 26 + c] = len(self.end)
self.end.append(False)
self.next.extend([0] * 26)
node = self.next[node * 26 + c]
self.end[node] = True
def _find(self, s):
node = 0
for ch in s:
node = self.next[node * 26 + ord(ch) - 97]
if node == 0: return -1
return node
def search(self, word):
node = self._find(word)
return node != -1 and self.end[node]
def starts_with(self, prefix):
return self._find(prefix) != -1
class Trie {
private next: number[] = new Array(26).fill(0);
private end: boolean[] = [false];
insert(word: string): void {
let node = 0;
for (const ch of word) {
const c = ch.charCodeAt(0) - 97;
if (this.next[node * 26 + c] === 0) {
this.next[node * 26 + c] = this.end.length;
this.end.push(false);
this.next.push(...new Array(26).fill(0));
}
node = this.next[node * 26 + c];
}
this.end[node] = true;
}
private find(s: string): number {
let node = 0;
for (const ch of s) {
node = this.next[node * 26 + ch.charCodeAt(0) - 97];
if (node === 0) return -1;
}
return node;
}
search(word: string): boolean {
const node = this.find(word);
return node !== -1 && this.end[node];
}
startsWith(prefix: string): boolean {
return this.find(prefix) !== -1;
}
}