-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamelCasePatternMatching.cpp
68 lines (63 loc) · 2.25 KB
/
CamelCasePatternMatching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Given a dictionary of words where each word follows CamelCase notation, print all words (in lexicographical order) in the dictionary that match with a given pattern consisting of uppercase characters only.
Example: GeeksForGeeks matches the pattern "GFG", because if we combine all the capital letters in GeeksForGeeks they become GFG.
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.
Example 1:
Input:
N=3
Dictionary=["WelcomeGeek",
"WelcomeToGeeksForGeeks","GeeksForGeeks"]
Pattern="WTG"
Output:
WelcomeToGeeksForGeeks
Explanation:
Since only WelcomeToGeeksForGeeks matches
the pattern, it is the only answer.
Example 2:
Input:
N=8
Dictionary=["Hi","Hello","HelloWorld",
"HiTech","HiGeek","HiTechWorld",
"HiTechCity","HiTechLab"]
Pattern="HA"
Output:
-1
Explanation:
Since the pattern matches none of the words
of the string,the output is -1.
Your Task:
You don't need to read input or print anything. Your Task is to complete the function CamelCase() which takes an integer N, a Vector of strings Dictionary and a string Pattern and returns the strings in the dictionary that match the pattern, if not found any return -1.
Expected Time Complexity: O(N*|S|) S=Longest string in Dictionary
Expected Auxillary Space: O(26*N)
*/
class Solution {
public:
vector<string> CamelCase(int N, vector<string> Dictionary, string Pattern) {
vector<string> result;
int m=Pattern.size();
for(auto word : Dictionary){
int n=word.size();
if(n<m) continue;
int i=0, j=0;
while(i<n and j<m){
if(word[i]>='A' and word[i]<='Z'){
if(word[i]==Pattern[j]){
i++;
j++;
}else{
break;
}
}else{
i++;
}
if(j==m){
result.push_back(word);
}
}
}
if(result.empty()){
result.push_back("-1");
}
return result;
}
};