CamelCase Pattern Matching

Last updated: 29th Aug, 2020

  

Problem Statment

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. Example - AlgorithmicTreasure.



Example 1:

Input:
2
8
Hi Hello HelloWorld HiTech HiGeek HiTechWorld HiTechCity HiTechLab
HA
3
Welcome WelcomeToAlgorithmicTreasure AlgorithmicTreasure
WTAT

Output:
No match found
WelcomeToAlgorithmicTreasure

Method:

  • Create a temp_result arrray and append the values in it if the letter is uppercase.
  • Check if the given pattern is in temp_result and the first letter and last letter in pattern is same as first and last letter in temp_result.
  • If all these conditions are satisfied then append temp_result to dictAns.
  • Then check the count.
  • If the count is zero then print no match found.
  • Otherwise result is equal to dictAns[pattern].
  • Sort the result, print the answer.

Python Code
                              
from collections import defaultdict
test=int(input())
for i in range(test):
    numWords= int(input())
    vectStr =input().split()
    pattern=input()
    count =0
    answer =[]
    dictAns= defaultdict(list)
    for j in vectStr:
        temp_result = "".join([c for c in j if(c.isupper())])
        if((pattern in temp_result) and (pattern[0]==temp_result[0]) and (pattern[-1]==temp_result[len(pattern)-1])):
            dictAns[pattern].append([temp_result,j])
            count=1
    if(count==0):
        print("No match found")
    else:
        result = dictAns[pattern]
        result.sort(key=lambda x:x[0])
        answer = [i[1] for i in result]
        print(" ".join(answer))