-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord Pattern
More file actions
39 lines (39 loc) · 1010 Bytes
/
Copy pathWord Pattern
File metadata and controls
39 lines (39 loc) · 1010 Bytes
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
class Solution {
public:
bool wordPattern(string pattern, string s) {
int n=pattern.size();
int m=s.size();
vector<string> ans;
int i=0;
while(i<m){
string st;
while(s[i]!=' ' && i<m){
st+=s[i];
i++;
}
i++;
ans.push_back(st);
}
int sz=ans.size();
if(sz!=n){
return false;
}
unordered_map<char,string> mp;
unordered_map<string,char> mp1;
for(int j=0;j<n;j++){
if(mp.find(pattern[j])!=mp.end()){
if(mp[pattern[j]]!=ans[j]){
return false;
}
}else if(mp1.find(ans[j])!=mp1.end()){
if(mp1[ans[j]]!=pattern[j]){
return false;
}
}else{
mp1[ans[j]]=pattern[j];
mp[pattern[j]]=ans[j];
}
}
return true;
}
};