Check if the given Trie contains words starting from every alphabet
Given a Trie, the task is to check if it contains words starting from every alphabet from [a – z].
Examples:
Input: keys[] = {“element”, “fog”, “great”, “hi”,
“ok”, “ios”, “parrot”, “quiz”, “kim”, “mango”, “nature”, “apple”,
“ball”, “cat”, “dog”, “lime”, “ruby”, “shine”, “tinkter”,
“ultra”, “volly”, “wow”, “xerox”, “yak”, “zenon”, “joke”}
Output: Yes
Input: keys[] = {“geeks”, “for”, “geeks”}
Output: No
Approach: We just need to focus on the root node of the given Trie and no need to traverse other nodes. We simply check if Trie has all its child pointers pointing to valid nodes i.e. there are words starting from every character of the alphabet.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int ALPHABET_SIZE = 26; // Trie node struct TrieNode { struct TrieNode* children[ALPHABET_SIZE]; // isEndOfWord is true if the node represents // end of a word bool isEndOfWord; }; // Returns new trie node (initialized to NULL) struct TrieNode* getNode(void) { struct TrieNode* pNode = new TrieNode; pNode->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) pNode->children[i] = NULL; return pNode; } // If not present, inserts key into trie // If the key is prefix of trie node, just // marks leaf node void insert(struct TrieNode* root, string key) { struct TrieNode* pCrawl = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (!pCrawl->children[index]) pCrawl->children[index] = getNode(); pCrawl = pCrawl->children[index]; } // Mark last node as leaf pCrawl->isEndOfWord = true; } // Function to check if Trie contains words // starting from all the alphabets bool containsAll(TrieNode* root) { // We check if root node has all childs // if None of them is NULL then we return true for (int i = 0; i < 26; i++) { // If there is no word in the trie starting // from the current alphabet if (root->children[i] == NULL) return false; } return true; } // Driver code int main() { string keys[] = { "element", "fog", "great", "hi", "ok", "ios", "parrot", "quiz", "kim", "mango", "nature", "apple", "ball", "cat", "dog", "lime", "ruby", "shine", "tinkter", "ultra", "volly", "wow", "xerox", "yak", "zenon", "joke" }; int n = sizeof(keys) / sizeof(keys[0]); // Create the Trie struct TrieNode* root = getNode(); for (int i = 0; i < n; i++) insert(root, keys[i]); // If trie contains words starting // from every alphabet if (containsAll(root)) cout << "Yes"; else cout << "No"; return 0; } |
Yes
Recommended Posts:
- Counting the number of words in a Trie
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Check if the given string of words can be formed from words present in the dictionary
- Boggle | Set 2 (Using Trie)
- Trie | (Delete)
- Bottom-up traversal of a Trie
- Search in a trie Recursively
- Persistent Trie | Set 1 (Introduction)
- Insertion in a Trie recursively
- Implement a Dictionary using Trie
- Trie | (Insert and Search)
- Trie | (Display Content)
- Trie memory optimization using hash map
- Count inversions in an array | Set 4 ( Using Trie )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



