class TrieNode { public: int count = 0; TrieNode* children[26] = {nullptr}; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const std::string& word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; node->count++; } } int countPrefix(const std::string& prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (!node->children[idx]) return 0; node = node->children[idx]; } return node->count; } }; int main() { Trie trie; trie.insert("apple"); trie.insert("app"); trie.insert("application"); trie.insert("apt"); std::cout << trie.countPrefix("app") << std::endl; return 0; }
The words inserted are "apple", "app", "application", and "apt".
Only "apple", "app", and "application" start with "app".
So the count for prefix "app" is 3.
class TrieNode { public: int count = 0; TrieNode* children[26] = {nullptr}; }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const std::string& word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; node->count++; } } int countPrefix(const std::string& prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (!node->children[idx]) return 0; node = node->children[idx]; } return node->count; } }; int main() { Trie trie; trie.insert("cat"); trie.insert("car"); trie.insert("cart"); std::cout << trie.countPrefix("bat") << std::endl; return 0; }
None of the inserted words "cat", "car", "cart" start with "bat".
So the count for prefix "bat" is 0.
class TrieNode { public: int count = 0; TrieNode* children[26]; TrieNode() { for (int i = 0; i < 26; i++) children[i] = nullptr; } }; class Trie { public: TrieNode* root; Trie() { root = new TrieNode(); } void insert(const std::string& word) { TrieNode* node = root; for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new TrieNode(); node = node->children[idx]; node->count++; } } int countPrefix(const std::string& prefix) { TrieNode* node = root; for (char c : prefix) { int idx = c - 'a'; if (!node->children[idx]) return 0; node = node->children[idx]; } return node->count; } }; int main() { Trie trie; trie.insert("apple"); trie.insert("app"); std::cout << trie.countPrefix("app") << std::endl; return 0; }
The children array is initialized to nullptr in the constructor, so no segmentation fault occurs.
The count increments correctly for "apple" and "app".
Output is 2 because two words start with "app".
Initially, three words start with "do": "dog", "door", "dorm".
Deleting "door" removes one word starting with "do".
So the count for prefix "do" decreases by 1.
The words are "test", "tester", "testing", "team", "teach".
All start with "te" including "team" and "teach".
So total 5 words start with "te".
But the question asks for count of words with prefix "te" which is 5.
Wait, options do not have 5 as correct answer except A.
Check carefully: all 5 start with "te".
So correct answer is 5 (option A).