-
Notifications
You must be signed in to change notification settings - Fork 6
/
0290.cpp
36 lines (27 loc) · 869 Bytes
/
0290.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
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char, string> match;
int i = 0;
int j = 0;
while(i < pattern.size() && j < s.size()){
string word = "";
while(j < s.size() && s[j] != ' '){
word += s[j++];
}
if(match.count(pattern[i])){
if(match[pattern[i]] != word)
return false;
}
else{
for(auto m : match)
if(m.second == word)
return false;
match[pattern[i]] = word;
}
i++;
j++;
}
return i == pattern.size() && j == s.size() + 1;
}
};