CamelCase Pattern Matching

Given a list 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.

Examples

Input: arr[] = [ “WelcomeGeek”, “WelcomeToGeeksForGeeks”, “GeeksForGeeks” ], pattern = “WTG”
Output: WelcomeToGeeksForGeeks
Explanation:
There is only one abbreviation for the given pattern i.e., WelcomeToGeeksForGeeks.

Input: arr[] = [ “Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab” ], pattern = “HA”
Output: No match found
Explanation:
There is no such abbreviation for the given pattern.

Approach:



  1. Traverse through every word and keep Hashing that word with every uppercase letter found in the given string.
    For Example:

    For string = "GeeksForGeeks"
    then the hashing after every uppercase letter found is:
    map {
         {G, GeeksForGeeks},
         {GF, GeeksForGeeks},
         {GFG, GeeksForGeeks}
    } 
    
  2. After creating hashing for all the string in the list. Search for the given pattern in the map and print all the string mapped to it.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ to find CamelCase Pattern
// matching
#include "bits/stdc++.h"
using namespace std;
  
// Function that prints the camel
// case pattern matching
void CamelCase(vector<string>& words,
               string pattern)
{
  
    // Map to store the hashing
    // of each words with every
    // uppercase letter found
    map<string, vector<string> > map;
  
    // Traverse the words array
    // that contains all the
    // string
    for (int i = 0; i < words.size(); i++) {
  
        // Intialise str as
        // empty
        string str = "";
  
        // length of string words[i]
        int l = words[i].length();
        for (int j = 0; j < l; j++) {
  
            // For every uppercase
            // letter found map
            // that uppercase to
            // original words
            if (words[i][j] >= 'A'
                && words[i][j] <= 'Z') {
                str += words[i][j];
                map[str].push_back(words[i]);
            }
        }
    }
  
    bool wordFound = false;
  
    // Traverse the map for pattern
    // matching
    for (auto& it : map) {
  
        // If pattern matches then
        // print the corresponding
        // mapped words
        if (it.first == pattern) {
            wordFound = true;
            for (auto& itt : it.second) {
                cout << itt << endl;
            }
        }
    }
  
    // If word not found print
    // "No match found"
    if (!wordFound) {
        cout << "No match found";
    }
}
  
// Driver's Code
int main()
{
    vector<string> words = {
        "Hi", "Hello", "HelloWorld",
        "HiTech", "HiGeek", "HiTechWorld",
        "HiTechCity", "HiTechLab"
    };
  
    // Pattern to be found
    string pattern = "HT";
  
    // Function call to find the
    // words that match to the
    // given pattern
    CamelCase(words, pattern);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java to find CamelCase Pattern
// matching
import java.util.*;
  
class GFG{
   
// Function that prints the camel
// case pattern matching
static void CamelCase(ArrayList<String> words,
               String pattern)
{
   
    // Map to store the hashing
    // of each words with every
    // uppercase letter found
    Map<String, List<String>> map = new HashMap<String, List<String>>();
   
    // Traverse the words array
    // that contains all the
    // String
    for (int i = 0; i < words.size(); i++) {
   
        // Intialise str as
        // empty
        String str = "";
   
        // length of String words[i]
        int l = words.get(i).length();
        for (int j = 0; j < l; j++) {
   
            // For every uppercase
            // letter found map
            // that uppercase to
            // original words
            if (words.get(i).charAt(j) >= 'A'
                && words.get(i).charAt(j) <= 'Z') {
                str += words.get(i).charAt(j);
                map.put(str,list(map.get(str),words.get(i)));
            }
        }
    }
   
    boolean wordFound = false;
   
    // Traverse the map for pattern
    // matching
    for (Map.Entry<String,List<String>> it : map.entrySet()) {
   
        // If pattern matches then
        // print the corresponding
        // mapped words
        if (it.getKey().equals(pattern)) {
            wordFound = true;
            for(String s : it.getValue())
            System.out.print(s +"\n");
              
        }
    }
   
    // If word not found print
    // "No match found"
    if (!wordFound) {
        System.out.print("No match found");
    }
}
   
private static List<String> list(List<String> list, String str) {
    List<String> temp = new ArrayList<String>();
    if(list != null)
        temp.addAll(list);
    temp.add(str);
    return temp;
}
  
// Driver's Code
public static void main(String[] args)
{
    String arr[] = {"Hi", "Hello", "HelloWorld",
            "HiTech", "HiGeek", "HiTechWorld",
            "HiTechCity", "HiTechLab"
        };
  
    ArrayList<String> words = new ArrayList<String>(Arrays.asList(arr));
   
    // Pattern to be found
    String pattern = "HT";
   
    // Function call to find the
    // words that match to the
    // given pattern
    CamelCase(words, pattern);
   
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 to find CamelCase Pattern 
# matching 
  
# Function that prints the camel 
# case pattern matching 
def CamelCase(words, pattern) :
  
    # Map to store the hashing 
    # of each words with every 
    # uppercase letter found 
    map = dict.fromkeys(words,None); 
  
    # Traverse the words array 
    # that contains all the 
    # string 
    for i in range(len(words)) :
  
        # Intialise str as 
        # empty 
        string = ""; 
  
        # length of string words[i] 
        l = len(words[i]); 
        for j in range(l) :
  
            # For every uppercase 
            # letter found map 
            # that uppercase to 
            # original words 
            if (words[i][j] >= 'A' and words[i][j] <= 'Z') :
                string += words[i][j]; 
                  
                if string not in map :
                    map[string] = [words[i]]
                      
                elif map[string] is None :
                    map[string] = [words[i]]
                      
                else :
                    map[string].append(words[i]); 
  
    wordFound = False
  
    # Traverse the map for pattern 
    # matching 
    for key,value in map.items() :
  
        # If pattern matches then 
        # print the corresponding 
        # mapped words 
        if (key == pattern) :
            wordFound = True
            for itt in value :
                print(itt); 
  
    # If word not found print 
    # "No match found" 
    if (not wordFound) :
        print("No match found"); 
   
  
# Driver's Code 
if __name__ == "__main__"
  
    words = [
        "Hi", "Hello", "HelloWorld"
        "HiTech", "HiGeek", "HiTechWorld"
        "HiTechCity", "HiTechLab"
    ]; 
  
    # Pattern to be found 
    pattern = "HT"
  
    # Function call to find the 
    # words that match to the 
    # given pattern 
    CamelCase(words, pattern); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

      
// C# to find CamelCase Pattern
// matching
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function that prints the camel
// case pattern matching
static void CamelCase(List<String> words,
               String pattern)
{
    
    // Map to store the hashing
    // of each words with every
    // uppercase letter found
    Dictionary<String, List<String>> map = new Dictionary<String, List<String>>();
    
    // Traverse the words array
    // that contains all the
    // String
    for (int i = 0; i < words.Count; i++) {
    
        // Intialise str as
        // empty
        String str = "";
    
        // length of String words[i]
        int l = words[i].Length;
        for (int j = 0; j < l; j++) {
    
            // For every uppercase
            // letter found map
            // that uppercase to
            // original words
            if (words[i][j] >= 'A'
                && words[i][j] <= 'Z') {
                str += words[i][j];
                if(map.ContainsKey(str))
                    map[str] = list(map[str],words[i]);
                else
                    map.Add(str,list(null,words[i]));
            }
        }
    }
    
    bool wordFound = false;
    
    // Traverse the map for pattern
    // matching
    foreach (KeyValuePair<String,List<String>> it in map) {
        // If pattern matches then
        // print the corresponding
        // mapped words
        if (it.Key.Equals(pattern)) {
            wordFound = true;
            foreach(String s in it.Value)
            Console.Write(s +"\n");
               
        }
    }
    
    // If word not found print
    // "No match found"
    if (!wordFound) {
        Console.Write("No match found");
    }
}
    
private static List<String> list(List<String> list, String str) {
    List<String> temp = new List<String>();
    if(list != null)
        temp.AddRange(list);
    temp.Add(str);
    return temp;
}
   
// Driver's Code
public static void Main(String[] args)
{
    String []arr = {"Hi", "Hello", "HelloWorld",
            "HiTech", "HiGeek", "HiTechWorld",
            "HiTechCity", "HiTechLab"
        };
   
    List<String> words = new List<String>(arr);
    
    // Pattern to be found
    String pattern = "HT";
    
    // Function call to find the
    // words that match to the
    // given pattern
    CamelCase(words, pattern);
    
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

HiTech
HiTechWorld
HiTechCity
HiTechLab

Time Complexity: O(N*M) where N is the length of list containing the strings and M is the length of longest string.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Hi , I like to code

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.