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++
#include "bits/stdc++.h"
using namespace std;
void CamelCase(vector<string>& words, string pattern)
{
map<string, vector<string> > map;
for ( int i = 0; i < words.size(); i++) {
string str = "" ;
int l = words[i].length();
for ( int j = 0; j < l; j++) {
if (words[i][j] >= 'A' && words[i][j] <= 'Z' ) {
str += words[i][j];
map[str].push_back(words[i]);
}
}
}
bool wordFound = false ;
for ( auto & it : map) {
if (it.first == pattern) {
wordFound = true ;
for ( auto & itt : it.second) {
cout << itt << endl;
}
}
}
if (!wordFound) {
cout << "No match found" ;
}
}
int main()
{
vector<string> words
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
string pattern = "HT" ;
CamelCase(words, pattern);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void CamelCase(ArrayList<String> words,
String pattern)
{
Map<String, List<String> > map
= new HashMap<String, List<String> >();
for ( int i = 0 ; i < words.size(); i++) {
String str = "" ;
int l = words.get(i).length();
for ( int j = 0 ; j < l; j++) {
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 ;
for (Map.Entry<String, List<String> > it :
map.entrySet()) {
if (it.getKey().equals(pattern)) {
wordFound = true ;
for (String s : it.getValue())
System.out.print(s + "\n" );
}
}
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;
}
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));
String pattern = "HT" ;
CamelCase(words, pattern);
}
}
|
Python3
def CamelCase(words, pattern):
map = dict .fromkeys(words, None )
for i in range ( len (words)):
string = ""
l = len (words[i])
for j in range (l):
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
for key, value in map .items():
if (key = = pattern):
wordFound = True
for itt in value:
print (itt)
if ( not wordFound):
print ( "No match found" )
if __name__ = = "__main__" :
words = [
"Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab"
]
pattern = "HT"
CamelCase(words, pattern)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void CamelCase(List<String> words,
String pattern)
{
Dictionary<String, List<String> > map
= new Dictionary<String, List<String> >();
for ( int i = 0; i < words.Count; i++) {
String str = "" ;
int l = words[i].Length;
for ( int j = 0; j < l; j++) {
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 ;
foreach (
KeyValuePair<String, List<String> > it in map)
{
if (it.Key.Equals(pattern)) {
wordFound = true ;
foreach (String s in it.Value)
Console.Write(s + "\n" );
}
}
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;
}
public static void Main(String[] args)
{
String[] arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
List<String> words = new List<String>(arr);
String pattern = "HT" ;
CamelCase(words, pattern);
}
}
|
Javascript
const CamelCase = (words, pattern) => {
const map = new Map();
for (let i = 0; i < words.length; i++) {
let str = "" ;
const l = words[i].length;
for (let j = 0; j < l; j++) {
if (words[i][j] >= "A" && words[i][j] <= "Z" ) {
str += words[i][j];
if (map.has(str)) {
const list = map.get(str);
list.push(words[i]);
map.set(str, list);
}
else {
map.set(str, [words[i]]);
}
}
}
}
let wordFound = false
|
Output
HiTech
HiTechWorld
HiTechCity
HiTechLab
Time Complexity: O(N*M) where N is the length of the list containing the strings and M is the length of the longest string.
Auxiliary Space: O(N*M)
Efficient Approach 1:
Step-by-step approach:
- Prepare a string by concatenating all array elements & semicolon as a delimiter after every array element.
- Traverse through concatenated string and look for Upper Case characters or delimiter.
- Hold a temporary string with all Upper Case characters until delimiter comes in traversal. Add this temporary string as a key (if key doesn’t exist) in dictionary or append the word if key already exists.
- Once delimiter reached, reset the temporary variables.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PrintMatchingCamelCase(vector<string> arr,
string pattern)
{
string cctdString = "" ;
for ( int i = 0; i < arr.size(); i++) {
cctdString += arr[i];
if (i != arr.size() - 1)
cctdString.push_back( ';' );
}
unordered_map<string, vector<string> > maps;
int charPos = 0;
int wordPos = 0;
string strr = "" ;
for (; charPos < cctdString.length(); charPos++) {
if (cctdString[charPos] >= 'A'
&& cctdString[charPos] <= 'Z' ) {
strr += cctdString[charPos];
if (maps.find(strr) != maps.end()) {
vector<string> temp;
temp.insert(temp.end(), maps[strr].begin(),
maps[strr].end());
temp.push_back(arr[wordPos]);
maps[strr] = temp;
}
else {
vector<string> vec = { arr[wordPos] };
maps[strr] = vec;
}
}
else if (cctdString[charPos] == ';' ) {
wordPos++;
strr = "" ;
}
}
if (maps.find(pattern) != maps.end()) {
for ( int i = 0; i < maps[pattern].size(); i++) {
cout << maps[pattern][i] << endl;
}
}
else {
cout << "No Match Found" << endl;
}
}
int main()
{
vector<string> arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
string pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
}
|
Java
import java.util.*;
public class Solution {
static void PrintMatchingCamelCase(String[] arr,
String pattern)
{
String cctdString = "" ;
for ( int i = 0 ; i < arr.length; i++) {
cctdString += arr[i];
if (i != arr.length - 1 )
cctdString += ';' ;
}
HashMap<String, ArrayList<String> > maps
= new HashMap<>();
int charPos = 0 ;
int wordPos = 0 ;
String strr = "" ;
for (; charPos < cctdString.length(); charPos++) {
if (cctdString.charAt(charPos) >= 'A'
&& cctdString.charAt(charPos) <= 'Z' ) {
strr += cctdString.charAt(charPos);
if (maps.containsKey(strr)) {
ArrayList<String> temp
= new ArrayList<>();
temp = maps.getOrDefault(
strr, new ArrayList<>());
temp.add(arr[wordPos]);
maps.put(strr, temp);
}
else {
ArrayList<String> temp
= new ArrayList<>();
temp.add(arr[wordPos]);
maps.put(strr, temp);
}
}
else if (cctdString.charAt(charPos) == ';' ) {
wordPos++;
strr = "" ;
}
}
if (maps.containsKey(pattern)) {
for ( int i = 0 ;
i < maps.getOrDefault(pattern,
new ArrayList<>())
.size();
i++) {
System.out.println(
maps.get(pattern).get(i));
}
}
else {
System.out.println( "No Match Found" );
}
}
public static void main(String[] args)
{
String[] arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
String pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
}
}
|
Python3
def PrintMatchingCamelCase(arr, pattern):
cctdString = ';' .join(arr)
maps = dict ()
wordPos = 0
strr = ""
for charPos in range ( len (cctdString)):
if (cctdString[charPos] > = 'A'
and cctdString[charPos] < = 'Z' ):
strr + = cctdString[charPos]
if strr in maps:
temp = []
temp.extend(maps[strr])
temp.append(arr[wordPos])
maps[strr] = temp
else :
vec = [arr[wordPos], ]
maps[strr] = vec
elif (cctdString[charPos] = = ';' ):
wordPos + = 1
strr = ""
if (pattern in maps):
for i in range ( len (maps[pattern])):
print (maps[pattern][i])
else :
print ( "No Match Found" )
if __name__ = = '__main__' :
arr = [ "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" ]
pattern = "HT"
PrintMatchingCamelCase(arr, pattern)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
public static void
PrintMatchingCamelCase(String[] arr, String pattern)
{
String cctdString
= arr.Aggregate((i, j) = > i + ';' + j);
Dictionary<String, List<String> > map
= new Dictionary< string , List< string > >();
int charPos = 0;
int wordPos = 0;
string strr = string .Empty;
for (; charPos < cctdString.Length; charPos++) {
if (cctdString[charPos] >= 'A'
&& cctdString[charPos] <= 'Z' ) {
strr += cctdString[charPos];
if (map.ContainsKey(strr)) {
List<String> temp = new List< string >();
temp.AddRange(map[strr]);
temp.Add(arr[wordPos]);
map[strr] = temp;
}
else {
map.Add(strr, new List< string >{
arr[wordPos] });
}
}
else if (cctdString[charPos] == ';' ) {
wordPos++;
strr = string .Empty;
}
}
if (map.ContainsKey(pattern)) {
foreach (String word in map[pattern])
{
Console.WriteLine(word);
}
}
else {
Console.WriteLine( "No Match Found" );
}
}
public static void Main(String[] args)
{
String[] arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
String pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
}
}
|
Javascript
function PrintMatchingCamelCase(arr, pattern) {
let cctdString = arr.join( ';' );
let maps = {};
let wordPos = 0;
let strr = '' ;
for (let charPos = 0; charPos < cctdString.length; charPos++) {
if (
cctdString[charPos] >= 'A' &&
cctdString[charPos] <= 'Z'
) {
strr += cctdString[charPos];
if (strr in maps) {
let temp = [];
temp = temp.concat(maps[strr]);
temp.push(arr[wordPos]);
maps[strr] = temp;
} else {
let vec = [arr[wordPos]];
maps[strr] = vec;
}
} else if (cctdString[charPos] === ';' ) {
wordPos += 1;
strr = '' ;
}
}
if (pattern in maps) {
for (let i = 0; i < maps[pattern].length; i++) {
console.log(maps[pattern][i]);
}
} else {
console.log( 'No Match Found' );
}
}
let arr = [
'Hi' ,
'Hello' ,
'HelloWorld' ,
'HiTech' ,
'HiGeek' ,
'HiTechWorld' ,
'HiTechCity' ,
'HiTechLab'
];
let pattern = 'HT' ;
PrintMatchingCamelCase(arr, pattern);
|
Output
HiTech
HiTechWorld
HiTechCity
HiTechLab
Time Complexity: O(N*|S|) N is size of array and |S| is max size of word in array.
Auxiliary Space: O(N)
Efficient Approach 2:
Step-by-step approach:
- Traverse through every word of given vector of string.
- For each word we initialize a variable x = 0 which indicate the index of pattern to be matched.
- Traverse the each word, if the character of word matched with x’th character of pattern then increment x, else if the character is not match and uppercase then further solution is not possible.
- If we able to match all character of pattern then add into our solution.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PrintMatchingCamelCase(vector<string> arr,
string pattern)
{
vector<string> ans;
for ( int i = 0; i < arr.size(); i++) {
int x = 0;
for ( int j = 0; j < arr[i].size(); j++) {
if (x < pattern.size()
&& pattern[x] == arr[i][j]) {
x++;
}
else if ( isupper (arr[i][j]))
break ;
}
if (x == pattern.size()) {
ans.push_back(arr[i]);
}
}
if (ans.size() == 0) {
cout << "No Match Found" << endl;
}
else {
for ( int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
}
}
int main()
{
vector<string> arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
string pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class CamelCaseMatcher {
public static void
printMatchingCamelCase(String[] arr, String pattern)
{
List<String> ans = new ArrayList<>();
for (String word : arr) {
int x = 0 ;
for ( char letter : word.toCharArray()) {
if (x < pattern.length()
&& pattern.charAt(x) == letter) {
x++;
}
else if (Character.isUpperCase(letter)) {
break ;
}
}
if (x == pattern.length()) {
ans.add(word);
}
}
if (ans.isEmpty()) {
System.out.println( "No Match Found" );
}
else {
for (String word : ans) {
System.out.println(word);
}
}
}
public static void main(String[] args)
{
String[] arr
= { "Hi" , "Hello" , "HelloWorld" ,
"HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
String pattern = "HT" ;
printMatchingCamelCase(arr, pattern);
}
}
|
Python3
import re
def PrintMatchingCamelCase(arr, pattern):
ans = []
for word in arr:
x = 0
for letter in word:
if x < len (pattern) and pattern[x] = = letter:
x + = 1
elif letter.isupper():
break
if x = = len (pattern):
ans.append(word)
if len (ans) = = 0 :
print ( "No Match Found" )
else :
for word in ans:
print (word)
arr = [ "Hi" , "Hello" , "HelloWorld" , "HiTech" , "HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" ]
pattern = "HT"
PrintMatchingCamelCase(arr, pattern)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void
PrintMatchingCamelCase(List< string > arr, string pattern)
{
List< string > ans = new List< string >();
foreach ( string word in arr)
{
int x = 0;
foreach ( char letter in word)
{
if (x < pattern.Length
&& pattern[x] == letter) {
x++;
}
else if ( char .IsUpper(letter)) {
break ;
}
}
if (x == pattern.Length) {
ans.Add(word);
}
}
if (ans.Count == 0) {
Console.WriteLine( "No Match Found" );
}
else {
foreach ( string word in ans)
{
Console.WriteLine(word);
}
}
}
public static void Main()
{
List< string > arr
= new List< string >{ "Hi" , "Hello" ,
"HelloWorld" , "HiTech" ,
"HiGeek" , "HiTechWorld" ,
"HiTechCity" , "HiTechLab" };
string pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
}
}
|
Javascript
const PrintMatchingCamelCase = (arr, pattern) => {
let ans = [];
for (let i = 0; i < arr.length; i++) {
let x = 0;
for (let j = 0; j < arr[i].length; j++) {
if (x < pattern.length && pattern[x] === arr[i][j]) {
x++;
}
else if (arr[i][j] === arr[i][j].toUpperCase()) {
break ;
}
}
if (x === pattern.length) {
ans.push(arr[i]);
}
}
if (ans.length === 0) {
console.log( "No Match Found" );
}
else {
for (let i = 0; i < ans.length; i++) {
console.log(ans[i]);
}
}
};
const arr = [
"Hi" ,
"Hello" ,
"HelloWorld" ,
"HiTech" ,
"HiGeek" ,
"HiTechWorld" ,
"HiTechCity" ,
"HiTechLab"
];
const pattern = "HT" ;
PrintMatchingCamelCase(arr, pattern);
|
Output
HiTech
HiTechWorld
HiTechCity
HiTechLab
Time Complexity: O(N*|S|) N is size of array and |S| is max size of word in array.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Oct, 2023
Like Article
Save Article