class Trie {
Map<Character, Trie> next = new HashMap<>();
boolean word;
void add(String text) {
Trie node = this;
for (char ch : text.toCharArray()) node = node.next.computeIfAbsent(ch, key -> new Trie());
node.word = true;
}
}
class Trie:
def __init__(self):
self.next = {}
self.word = False
def add(self, text: str) -> None:
node = self
for ch in text:
node = node.next.setdefault(ch, Trie())
node.word = True
final class Trie:
val next = scala.collection.mutable.Map[Char, Trie]()
var word = false
def add(text: String): Unit =
var node = this
for ch <- text do node = node.next.getOrElseUpdate(ch, new Trie)
node.word = true
class Trie {
std::unordered_map<char, Trie*> next;
bool word = false;
public:
void add(const std::string& text) {
Trie* node = this;
for (char ch : text) {
if (!node->next.count(ch)) node->next[ch] = new Trie();
node = node->next[ch];
}
node->word = true;
}
};