-
Notifications
You must be signed in to change notification settings - Fork 0
/
CamelCase Pattern Matching.cpp
67 lines (63 loc) · 1.71 KB
/
CamelCase Pattern Matching.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<string> CamelCase(int N, vector<string> Dictionary, string Pattern) {
// code here
vector<string>ans;
map<string,vector<string>>mapping;
for(int i=0;i<N;i++){
string pat="";
for(int j=0;j<Dictionary[i].length();j++){
if(Dictionary[i][j]>='A' && Dictionary[i][j]<='Z'){
pat+=Dictionary[i][j];
}
}
mapping[pat].push_back(Dictionary[i]);
}
for(auto i : mapping){
int j=0;
bool flag=true;
int k=0;
string temp=i.first;
while(j<Pattern.length() && k<i.first.length()){
if(Pattern[j]!=temp[k]) {
flag=false;
break;
}else{
j++;
k++;
}
}
if(flag && j==Pattern.length()){
for(auto j : i.second){
ans.push_back(j);
}
}
}
if(ans.size()==0) return {"-1"};
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
vector<string> Dictionary(N);
for (int i = 0; i < N; i++) cin >> Dictionary[i];
string Pattern;
cin >> Pattern;
Solution ob;
vector<string> ans = ob.CamelCase(N, Dictionary, Pattern);
sort(ans.begin(), ans.end());
for (auto u : ans) cout << u << " ";
cout << "\n";
}
return 0;
}
// } Driver Code Ends