aboutsummaryrefslogtreecommitdiff
path: root/other/trie_prefix.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/trie_prefix.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/trie_prefix.cpp')
-rw-r--r--other/trie_prefix.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/other/trie_prefix.cpp b/other/trie_prefix.cpp
new file mode 100644
index 0000000..b56e38b
--- /dev/null
+++ b/other/trie_prefix.cpp
@@ -0,0 +1,45 @@
+// 208. Implement Trie (Prefix Tree)
+#include "leetcode.h"
+
+class Trie {
+public:
+ Trie() {}
+ void insert(string word) {
+ Trie *node = this;
+ for (char c : word) {
+ if (!node->next.count(c))
+ node->next[c] = new Trie();
+ node = node->next[c];
+ }
+ node->is_word = true;
+ }
+ bool search(string word) {
+ Trie *node = this;
+ for (char c : word) {
+ if (!node->next.count(c))
+ return false;
+ node = node->next[c];
+ }
+ return node->is_word;
+ }
+ bool startsWith(string prefix) {
+ Trie *node = this;
+ for (char c : prefix) {
+ if (!node->next.count(c))
+ return false;
+ node = node->next[c];
+ }
+ return true;
+ }
+
+private:
+ map<char, Trie *> next = {};
+ bool is_word = false;
+};
+
+int main() {
+ Trie *obj = new Trie();
+ obj->insert("Trie");
+ bool param_2 = obj->search("Trie");
+ bool param_3 = obj->startsWith("Trie");
+}