Print all words matching a pattern in CamelCase Notation Dictionary
Given a dictionary of words where each word follows CamelCase notation, print all words in the dictionary that match with a given pattern consisting of uppercase characters only.
CamelCase is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. Common examples include: “PowerPoint” and “WikiPedia”, “GeeksForGeeks”, “CodeBlocks”, etc.
Input: dict = ["Hi", "Hello", "HelloWorld", "HiTech", "HiGeek", "HiTechWorld", "HiTechCity", "HiTechLab"] For pattern "HT", Output: ["HiTech", "HiTechWorld", "HiTechCity", "HiTechLab"] For pattern "H", Output: ["Hi", "Hello", "HelloWorld", "HiTech", "HiGeek", "HiTechWorld", "HiTechCity", "HiTechLab"] For pattern "HTC", Output: ["HiTechCity"] Input: dict = ["WelcomeGeek","WelcomeToGeeksForGeeks", "GeeksForGeeks"] For pattern "WTG", Output: ["WelcomeToGeeksForGeeks"] For pattern "GFG", Output: [GeeksForGeeks] For pattern "GG", Output: No match found
The idea is to insert all dictionary keys into the Trie one by one. Here key refers to only Uppercase characters in original word in CamelCase notation. If we encounter the key for the first time, we need to mark the last node as leaf node and insert the complete word for that key into the vector associated with the leaf node. If we encounter a key that is already in the trie, we update the vector associated with the leaf node with current word. After all dictionary words are processed, we search for the pattern in the trie and print all words that matches the pattern.
Below is the implementation of above idea –
HiTech HiTechCity HiTechLab HiTechWorld
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.