-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt41.cpp
78 lines (65 loc) · 1.8 KB
/
lt41.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
69
70
71
72
73
74
75
76
77
78
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char,string> bind;
unordered_map<string,int>check;
vector<string> holder;
string result;
int n=s.size();
//counting words in string s
int wordcount=0;
bool outsideword=true;
for(int i=0 ; i<n ;i++){
if(s[i]==' ' ){
outsideword=true;
holder.push_back(result);
result.clear();
}
if(s[i]!=' ' && outsideword==true){
outsideword=false;
wordcount++;
}
if(outsideword==false){
result.push_back(s[i]);
}
}
holder.push_back(result);
if(wordcount!=pattern.size()){
return false;
}
else{
int m=pattern.size();
int count=0;
bool match=true;
for(int i=0 ;i<m;i++){
if(bind[pattern[i]]=="\0"){
if(check[holder[i]]!=1){
match=true;
bind[pattern[i]]=holder[i];
check[holder[i]]=1;
}
else{
match=false;
break;
}
}
else{
if(bind[pattern[i]]!=holder[i]){
match=false;
break;
}
else{
match=true;
continue;
}
}
}
if(match==true){
return true;
}
else{
return false;
}
}
}
};