Given an array arr[] of strings, the task is to print the strings from the array which are not prefix of any other string from the same array.
Examples:
Input: arr[] = {“apple”, “app”, “there”, “the”, “like”}
Output:
apple
like
there
Here “app” is a prefix of “apple”
Hence, it is not printed and
“the” is a prefix of “there”Input: arr[] = {“a”, “aa”, “aaa”, “aaaa”}
Output:
aaaa
Naive approach: For every string of the array, we check if it is prefix of any other string. If it is then don’t display it.
Efficient approach: We pick strings from array one by one and insert it into Trie. Then there are two cases for the insertion of the string:
- While inserting if we found that the picked string is a prefix of an already inserted string then we don’t insert this string into the Trie.
- If a prefix is inserted first into the Trie and afterwards we find that the string is a prefix of some word then we simply make isEndOfWord = false for that particular node.
After constructing the Trie, we traverse it and display all the words in the Trie.
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 NULLs) 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; } // Function to insert a string into trie 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(); // While inerting a word make // each isEndOfWord as false pCrawl->isEndOfWord = false; pCrawl = pCrawl->children[index]; } int i; // Check if this word is prefix of // some already inserted word // If it is then don't insert this word for (i = 0; i < 26; i++) { if (pCrawl->children[i]) { break; } } // If present word is not prefix of // any other word then insert it if (i == 26) { pCrawl->isEndOfWord = true; } } // Function to display words in Trie void display(struct TrieNode* root, char str[], int level) { // If node is leaf node, it indicates end // of string, so a null character is added // and string is displayed if (root->isEndOfWord) { str[level] = '\0'; cout << str << endl; } int i; for (i = 0; i < ALPHABET_SIZE; i++) { // If NON NULL child is found // add parent key to str and // call the display function recursively // for child node if (root->children[i]) { str[level] = i + 'a'; display(root->children[i], str, level + 1); } } } // Driver code int main() { string keys[] = { "apple", "app", "there", "the", "like" }; int n = sizeof(keys) / sizeof(string); struct TrieNode* root = getNode(); // Construct trie for (int i = 0; i < n; i++) insert(root, keys[i]); char str[100]; display(root, str, 0); return 0; } |
apple like there
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Longest string which is prefix string of at least two strings
- Count of lists which are not a subset of any other given lists
- Longest string in an array which matches with prefix of the given string
- Number of sub-strings which are anagram of any sub-string of another string
- String matching with * (that matches with any) in any of the two strings
- Print the longest prefix of the given string which is also the suffix of the same string
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Sub-strings of a string that are prefix of the same string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Check if there exists any sub-sequence in a string which is not palindrome
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- A Program to check if strings are rotations of each other or not
- Check if strings are rotations of each other or not | Set 2
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- String from prefix and suffix of given two strings
- Count of strings whose prefix match with the given string to a given length k
- Find the longest string that can be made up of other strings from the array
- Sort an array of strings lexicographically based on prefix
- Length of longest common prefix possible by rearranging strings in a given array
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.
Improved By : ManasChhabra2

