From 1126c4062a20a5488d4268a1ec844d758ae6ee88 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 13 Sep 2022 09:05:00 +0800 Subject: [PATCH] Create 211.cpp --- LeetCode-CN/211.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 LeetCode-CN/211.cpp diff --git a/LeetCode-CN/211.cpp b/LeetCode-CN/211.cpp new file mode 100644 index 0000000..4a39722 --- /dev/null +++ b/LeetCode-CN/211.cpp @@ -0,0 +1,86 @@ + +class Trie { +public: + struct Node + { + bool ends; + Node* next[26]; + + Node() + { + ends = false; + memset(next, 0, sizeof(next)); + } + }; + Node* root; + + Trie() { + root = new Node; + } + + inline void stepDownInsert(const string& word, int index, Node* current) { + if (index >= word.size()) { + current->ends = true; + return; + } + + if (!current->next[word[index] - 'a']) { + current->next[word[index] - 'a'] = new Node; + } + stepDownInsert(word, index + 1, current->next[word[index] - 'a']); + } + + void insert(string word) { + stepDownInsert(word, 0, root); + } + + inline bool stepDownSearch(const string& word, int index, Node* current) { + if (index >= word.size()) return current->ends; + if (current->next[word[index] - 'a']) return stepDownSearch(word, index + 1, current->next[word[index] - 'a']); + return false; + } + + bool search(string word) { + return stepDownSearch(word, 0, root); + } + + inline bool stepDownPrefix(const string& word, int index, Node* current) { + if (index >= word.size()) return true; + if (current->next[word[index] - 'a']) return stepDownPrefix(word, index + 1, current->next[word[index] - 'a']); + return false; + } + + bool startsWith(string prefix) { + return stepDownPrefix(prefix, 0, root); + } +}; + +class WordDictionary { +public: + Trie t; + WordDictionary() { + + } + + void addWord(string word) { + t.insert(word); + } + + inline bool stepDownSearch(const string& word, int index, Trie::Node* current) { + if (index >= word.size()) return current->ends; + if (word[index] == '.') { + for (int i = 0; i < 26; i++) + { + if (current->next[i] && stepDownSearch(word, index + 1, current->next[i])) return true; + } + return false; + } + + if (current->next[word[index] - 'a']) return stepDownSearch(word, index + 1, current->next[word[index] - 'a']); + return false; + } + + bool search(string word) { + return stepDownSearch(word, 0, t.root); + } +};