Print all words matching a pattern in CamelCase Notation Dictionary
Given a dictionary of words where each word follows CamelCase notation, The task is to print all words in the dictionary that match with a given pattern consisting of uppercase characters only.
Note: 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.
Examples:
Input: dict[] = [“Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab”], pattern =”HT”,
Output: [“HiTech”, “HiTechWorld”, “HiTechCity”, “HiTechLab”]Input: dict[] = [“Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab”], pattern =”H”,
Output: [“Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab”]Input: dict[] = [“Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab”], pattern = “HTC”,
Output: [“HiTechCity”]Input: dict[] = [“WelcomeGeek”,”WelcomeToGeeksForGeeks”, “GeeksForGeeks”], pattern = “WTG”,
Output: [“WelcomeToGeeksForGeeks”]Input: dict[] = [“WelcomeGeek”,”WelcomeToGeeksForGeeks”, “GeeksForGeeks”], pattern = “GFG”,
Output: [GeeksForGeeks]Input: dict[] = [“WelcomeGeek”,”WelcomeToGeeksForGeeks”, “GeeksForGeeks”], pattern = “GG”,
Output: No match found
Find Pattern in Camelcase Notation using Trie:
Below is the idea to solve the problem:
Insert all dictionary keys into the Trie one by one. Here key refers to only Uppercase characters in original word in CamelCase notation.
- When encountering the key for the first time, mark the last node as leaf node and insert the complete word for that key into the vector associated with the leaf node.
- If encountering a key that is already in the trie, update the vector associated with the leaf node with current word.
After all dictionary words are processed, search for the pattern in the trie and print all words that match the pattern.
Follow the below steps to Implement the idea:
- Initialize a trie and insert Capital case letters in all the words in the trie.
- After inserting the word set bool isleaf to 1 and insert the word in the vector of string for the node of last character.
- Query for the pattern in trie and on the last character node of the pattern return the vector of strings.
Below is the implementation of the above idea:
C++
// C++ program to print all words in the CamelCase // dictionary that matches with a given pattern #include <bits/stdc++.h> using namespace std; // Alphabet size (# of upper-Case characters) #define ALPHABET_SIZE 26 // A Trie node struct TrieNode { TrieNode* children[ALPHABET_SIZE]; // isLeaf is true if the node represents // end of a word bool isLeaf; // vector to store list of complete words // in leaf node list<string> word; }; // Returns new Trie node (initialized to NULLs) TrieNode* getNewTrieNode( void ) { TrieNode* pNode = new TrieNode; if (pNode) { pNode->isLeaf = false ; for ( int i = 0; i < ALPHABET_SIZE; i++) pNode->children[i] = NULL; } return pNode; } // Function to insert word into the Trie void insert(TrieNode* root, string word) { int index; TrieNode* pCrawl = root; for ( int level = 0; level < word.length(); level++) { // consider only uppercase characters if ( islower (word[level])) continue ; // get current character position index = int (word[level]) - 'A' ; if (!pCrawl->children[index]) pCrawl->children[index] = getNewTrieNode(); pCrawl = pCrawl->children[index]; } // mark last node as leaf pCrawl->isLeaf = true ; // push word into vector associated with leaf node (pCrawl->word).push_back(word); } // Function to print all children of Trie node root void printAllWords(TrieNode* root) { // if current node is leaf if (root->isLeaf) { for (string str : root->word) cout << str << endl; } // recurse for all children of root node for ( int i = 0; i < ALPHABET_SIZE; i++) { TrieNode* child = root->children[i]; if (child) printAllWords(child); } } // search for pattern in Trie and print all words // matching that pattern bool search(TrieNode* root, string pattern) { int index; TrieNode* pCrawl = root; for ( int level = 0; level < pattern.length(); level++) { index = int (pattern[level]) - 'A' ; // Invalid pattern if (!pCrawl->children[index]) return false ; pCrawl = pCrawl->children[index]; } // print all words matching that pattern printAllWords(pCrawl); return true ; } // Main function to print all words in the CamelCase // dictionary that matches with a given pattern void findAllWords(vector<string> dict, string pattern) { // construct Trie root node TrieNode* root = getNewTrieNode(); // Construct Trie from given dict for (string word : dict) insert(root, word); // search for pattern in Trie if (!search(root, pattern)) cout << "No match found" ; } // Driver function int main() { // dictionary of words where each word follows // CamelCase notation vector<string> dict = { "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" }; // pattern consisting of uppercase characters only string pattern = "HT" ; findAllWords(dict, pattern); return 0; } |
Java
// Java program to print all words in the CamelCase // dictionary that matches with a given pattern import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CamelCase { // Alphabet size (# of upper-Case characters) static final int ALPHABET_SIZE = 26 ; // A Trie node static class TrieNode { TrieNode[] children = new TrieNode[ALPHABET_SIZE]; // isLeaf is true if the node represents // end of a word boolean isLeaf; // vector to store list of complete words // in leaf node List<String> word; public TrieNode() { isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE; i++) children[i] = null ; word = new ArrayList<String>(); } } static TrieNode root; // Function to insert word into the Trie static void insert(String word) { int index; TrieNode pCrawl = root; for ( int level = 0 ; level < word.length(); level++) { // consider only uppercase characters if (Character.isLowerCase(word.charAt(level))) continue ; // get current character position index = word.charAt(level) - 'A' ; if (pCrawl.children[index] == null ) pCrawl.children[index] = new TrieNode(); pCrawl = pCrawl.children[index]; } // mark last node as leaf pCrawl.isLeaf = true ; // push word into vector associated with leaf node (pCrawl.word).add(word); } // Function to print all children of Trie node root static void printAllWords(TrieNode root) { // if current node is leaf if (root.isLeaf) { for (String str : root.word) System.out.println(str); } // recurse for all children of root node for ( int i = 0 ; i < ALPHABET_SIZE; i++) { TrieNode child = root.children[i]; if (child != null ) printAllWords(child); } } // search for pattern in Trie and print all words // matching that pattern static boolean search(String pattern) { int index; TrieNode pCrawl = root; for ( int level = 0 ; level < pattern.length(); level++) { index = pattern.charAt(level) - 'A' ; // Invalid pattern if (pCrawl.children[index] == null ) return false ; pCrawl = pCrawl.children[index]; } // print all words matching that pattern printAllWords(pCrawl); return true ; } // Main function to print all words in the CamelCase // dictionary that matches with a given pattern static void findAllWords(List<String> dict, String pattern) { // construct Trie root node root = new TrieNode(); // Construct Trie from given dict for (String word : dict) insert(word); // search for pattern in Trie if (!search(pattern)) System.out.println( "No match found" ); } // Driver function public static void main(String args[]) { // dictionary of words where each word follows // CamelCase notation List<String> dict = Arrays.asList( "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" ); // pattern consisting of uppercase characters only String pattern = "HT" ; findAllWords(dict, pattern); } } // This code is contributed by Sumit Ghosh |
C#
// C# program to print all words in // the CamelCase dictionary that // matches with a given pattern using System; using System.Collections.Generic; class GFG { // Alphabet size (# of upper-Case characters) static int ALPHABET_SIZE = 26; // A Trie node public class TrieNode { public TrieNode[] children = new TrieNode[ALPHABET_SIZE]; // isLeaf is true if the node represents // end of a word public bool isLeaf; // vector to store list of complete words // in leaf node public List<String> word; public TrieNode() { isLeaf = false ; for ( int i = 0; i < ALPHABET_SIZE; i++) children[i] = null ; word = new List<String>(); } } static TrieNode root; // Function to insert word into the Trie static void insert(String word) { int index; TrieNode pCrawl = root; for ( int level = 0; level < word.Length; level++) { // consider only uppercase characters if ( char .IsLower(word[level])) continue ; // get current character position index = word[level] - 'A' ; if (pCrawl.children[index] == null ) pCrawl.children[index] = new TrieNode(); pCrawl = pCrawl.children[index]; } // mark last node as leaf pCrawl.isLeaf = true ; // push word into vector // associated with leaf node (pCrawl.word).Add(word); } // Function to print all children // of Trie node root static void printAllWords(TrieNode root) { // if current node is leaf if (root.isLeaf) { foreach (String str in root.word) Console.WriteLine(str); } // recurse for all children of root node for ( int i = 0; i < ALPHABET_SIZE; i++) { TrieNode child = root.children[i]; if (child != null ) printAllWords(child); } } // search for pattern in Trie and // print all words matching that pattern static bool search(String pattern) { int index; TrieNode pCrawl = root; for ( int level = 0; level < pattern.Length; level++) { index = pattern[level] - 'A' ; // Invalid pattern if (pCrawl.children[index] == null ) return false ; pCrawl = pCrawl.children[index]; } // print all words matching that pattern printAllWords(pCrawl); return true ; } // Main function to print all words // in the CamelCase dictionary that // matches with a given pattern static void findAllWords(List<String> dict, String pattern) { // construct Trie root node root = new TrieNode(); // Construct Trie from given dict foreach (String word in dict) insert(word); // search for pattern in Trie if (!search(pattern)) Console.WriteLine( "No match found" ); } // Driver Code public static void Main(String []args) { // dictionary of words where each word follows // CamelCase notation List<String> dict = new List<String>{ "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" }; // pattern consisting of // uppercase characters only String pattern = "HT" ; findAllWords(dict, pattern); } } // This code is contributed by Princi Singh |
Python3
import string # Alphabet size (# of upper-Case characters) ALPHABET_SIZE = 26 class TrieNode: def __init__( self ): self .children = [ None ] * ALPHABET_SIZE self .isLeaf = False self .word = [] # Returns new Trie node (initialized to NULLs) def getNewTrieNode(): pNode = TrieNode() return pNode # Function to insert word into the Trie def insert(root, word): pCrawl = root for level in range ( len (word)): # consider only uppercase characters if word[level].islower(): continue # get current character position index = ord (word[level]) - ord ( 'A' ) if not pCrawl.children[index]: pCrawl.children[index] = getNewTrieNode() pCrawl = pCrawl.children[index] # mark last node as leaf pCrawl.isLeaf = True # push word into vector associated with leaf node (pCrawl.word).append(word) # Function to print all children of Trie node root def printAllWords(root): # if current node is leaf if root.isLeaf: for str in root.word: print ( str ) # recurse for all children of root node for i in range (ALPHABET_SIZE): child = root.children[i] if child: printAllWords(child) # search for pattern in Trie and print all words # matching that pattern def search(root, pattern): pCrawl = root for level in range ( len (pattern)): index = ord (pattern[level]) - ord ( 'A' ) # Invalid pattern if not pCrawl.children[index]: return False pCrawl = pCrawl.children[index] # print all words matching that pattern printAllWords(pCrawl) return True # Main function to print all words in the CamelCase # dictionary that matches with a given pattern def findAllWords( dict , pattern): # construct Trie root node root = getNewTrieNode() # Construct Trie from given dict for word in dict : insert(root, word) # search for pattern in Trie if not search(root, pattern): print ( "No match found" ) # Driver function if __name__ = = '__main__' : # dictionary of words where each word follows # CamelCase notation dict = [ "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" ] # pattern consisting of uppercase characters only pattern = "HT" findAllWords( dict , pattern) |
Javascript
// Alphabet size (of upper-Case characters) const ALPHABET_SIZE = 26; // Class definition for TrieNode class TrieNode { constructor() { // Children array to store all the children nodes this .children = Array(ALPHABET_SIZE).fill( null ); // isLeaf property to check if the node is end of word this .isLeaf = false ; // word property to store the word at this node this .word = []; } } // Function to get a new TrieNode function getNewTrieNode() { const pNode = new TrieNode(); return pNode; } // Function to insert a word into the Trie function insert(root, word) { let pCrawl = root; // Traverse through each character of the word for (let level = 0; level < word.length; level++) { // Skip the character if it's not an uppercase letter if (!word[level].match(/[A-Z]/)) { continue ; } // Get the index of the character const index = word.charCodeAt(level) - 'A '.charCodeAt(0); // If there is no node for this character, create a new one if (!pCrawl.children[index]) { pCrawl.children[index] = getNewTrieNode(); } // Move the pointer to the child node pCrawl = pCrawl.children[index]; } // Mark the node as end of word pCrawl.isLeaf = true; // Store the word at this node pCrawl.word.push(word); } // Function to print all the words stored at a TrieNode function printAllWords(root) { // If the node is end of word, print all the words stored at this node if (root.isLeaf) { root.word.forEach((str) => { console.log(str); }); } // Traverse through all the children nodes for (let i = 0; i < ALPHABET_SIZE; i++) { const child = root.children[i]; // If there is a child node, call printAllWords for this node if (child) { printAllWords(child); } } } // Function to search for a pattern in Trie function search(root, pattern) { let pCrawl = root; // Traverse through each character of the pattern for (let level = 0; level < pattern.length; level++) { // Get the index of the character const index = pattern.charCodeAt(level) - ' A'.charCodeAt(0); // If there is no node for this character, return false if (!pCrawl.children[index]) { return false ; } // Move the pointer to the child node pCrawl = pCrawl.children[index]; } // If the pattern is found, print all the words stored at the end node printAllWords(pCrawl); return true ; } // function to find all the words matching the pattern function findAllWords(dict, pattern) { // construct Trie root node const root = getNewTrieNode(); // Construct Trie from given dict dict.forEach((word) => { insert(root, word); }); // search for pattern in Trie if (!search(root, pattern)) { console.log( "No match found" ); } } // main function to test the implementation // dictionary of words where each word follows // CamelCase notation const dict = [ "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" ]; // pattern consisting of uppercase characters only const pattern = "HT" ; findAllWords(dict, pattern); |
HiTech HiTechCity HiTechLab HiTechWorld
Time complexity: O(n*m) where m is the length of the pattern and n is the length of the dictionary.
Auxiliary Space: O(n)
Another Approach: Two Pointer
Approach/Intuition:
Here, we are checking if the current word matches with the pattern or not. If it matches then we store that string. For checking we are using two pointer approach:
- If the character in ‘s’ matches the current character in ‘pattern’, then the function moves on to the next character in ‘pattern’.
- If the character in ‘s’ is uppercase and does not match the current character in ‘pattern’, then the returns false. Returns true if it has reached the end of ‘s’ and matched every character in ‘pattern’, and false otherwise.
Follow the below steps to Implement the idea:
- Create an empty vector to store the words that match with the given pattern.
- For each word in Dictionary, it checks whether the word matches with pattern. If it does, insert that string in the answer and return the vector.
- In “check” function Initializes an integer “j” to 0, representing the current index of the pattern “pattern” that we are matching against
- If the character in “s” matches the current character in “pattern”, then increment “j” to move on to the next character in “pattern”.
- If the character in “s” is uppercase and does not match the current character in “pattern”, then return false because it means we have encountered an uppercase letter in “s” that does not match the corresponding character in “pattern”.
- Returns true if we have reached the end of “s” and matched every character in “pattern”, and false otherwise
Below is the code to implement the above approach:
C++
// C++ program to print all words in the CamelCase // dictionary that matches with a given pattern #include <bits/stdc++.h> using namespace std; bool check(string& s, string& pattern) { // Initializes an integer "j" to 0, representing the // current index of the pattern "pattern" that we are // matching against int j = 0; int n = s.size(); int m = pattern.size(); for ( int i = 0; i < n; i++) { // If the character in "s" matches the current // character in "pattern", then increment "j" to // move on to the next character in "pattern" if (s[i] == pattern[j]) { j++; // If the character in "s" is uppercase and does // not match the current character in "pattern", // then return false because it means we have // encountered an uppercase letter in "s" that // does not match the corresponding character in // "pattern" } else if ( isupper (s[i]) && j != m) { return false ; } } // Returns true if we have reached the end of "s" and // matched every character in "pattern", and false // otherwise return j == m; } vector<string> CamelCase(vector<string> dict, string pattern) { // Initializes an empty vector called "ans" to store the // words that match the pattern vector<string> ans; for ( auto word : dict) { // Calls the "check" function to determine whether // the word matches the pattern "pattern" if (check(word, pattern)) { // If the word matches the pattern, add it to // the "ans" vector ans.push_back(word); } } // Returns the "ans" vector containing the words that // match the pattern return ans; } // Driver function int main() { // dictionary of words where each word follows // CamelCase notation vector<string> dict = { "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" }; // pattern consisting of uppercase characters only string pattern = "HT" ; // Call the CamelCase function and store the result in // "ans" vector<string> ans = CamelCase(dict, pattern); // Print the elements of the "ans" vector for ( auto word : ans) { cout << word << endl; } return 0; } // this code is contributed by Ravi Singh |
Python3
def check(s, pattern): # Initializes an integer "j" to 0, representing the # current index of the pattern "pattern" that we are # matching against j = 0 n = len (s) m = len (pattern) for i in range (n): # If the character in "s" matches the current # character in "pattern", then increment "j" to # move on to the next character in "pattern" if j<m and s[i] = = pattern[j]: j + = 1 # If the character in "s" is uppercase and does # not match the current character in "pattern", # then return false because it means we have # encountered an uppercase letter in "s" that # does not match the corresponding character in # "pattern" elif s[i].isupper() and j ! = m: return False # Returns true if we have reached the end of "s" and # matched every character in "pattern", and false # otherwise return j = = m def CamelCase( dict , pattern): # Initializes an empty list called "ans" to store the # words that match the pattern ans = [] for word in dict : # Calls the "check" function to determine whether # the word matches the pattern "pattern" if check(word, pattern): # If the word matches the pattern, add it to # the "ans" list ans.append(word) # Returns the "ans" list containing the words that # match the pattern return ans # Driver function if __name__ = = '__main__' : # dictionary of words where each word follows # CamelCase notation dict = [ "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" ] # pattern consisting of uppercase characters only pattern = "HT" # Call the CamelCase function and store the result in # "ans" ans = CamelCase( dict , pattern) # Print the elements of the "ans" list for word in ans: print (word) |
Java
import java.util.*; class Main { static boolean check(String s, String pattern) { // Initializes an integer "j" to 0, representing the // current index of the pattern "pattern" that we are // matching against int j = 0 ; int n = s.length(); int m = pattern.length(); for ( int i = 0 ; i < n; i++) { // If the character in "s" matches the current // character in "pattern", then increment "j" to // move on to the next character in "pattern" if (j<m && s.charAt(i) == pattern.charAt(j)) { j++; // If the character in "s" is uppercase and does // not match the current character in "pattern", // then return false because it means we have // encountered an uppercase letter in "s" that // does not match the corresponding character in // "pattern" } else if (Character.isUpperCase(s.charAt(i)) && j != m) { return false ; } } // Returns true if we have reached the end of "s" and // matched every character in "pattern", and false // otherwise return j == m; } static List<String> CamelCase(List<String> dict, String pattern) { // Initializes an empty list called "ans" to store the // words that match the pattern List<String> ans = new ArrayList<>(); for (String word : dict) { // Calls the "check" function to determine whether // the word matches the pattern "pattern" if (check(word, pattern)) { // If the word matches the pattern, add it to // the "ans" list ans.add(word); } } // Returns the "ans" list containing the words that // match the pattern return ans; } // Driver function public static void main(String[] args) { // dictionary of words where each word follows // CamelCase notation List<String> dict = Arrays.asList( "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" , "HiTechCity" , "HiTechLab" ); // pattern consisting of uppercase characters only String pattern = "HT" ; // Call the CamelCase function and store the result in // "ans" List<String> ans = CamelCase(dict, pattern); // Print the elements of the "ans" list for (String word : ans) { System.out.println(word); } } } |
HiTech HiTechWorld HiTechCity HiTechLab
Time complexity: O(n * m), where n is the number of words in the dictionary and m is the length of the pattern
Auxiliary Space: O(k), where k is the number of words in the dictionary that match the pattern
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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Login to comment...