-
Notifications
You must be signed in to change notification settings - Fork 0
/
290_Word-Pattern.cpp
37 lines (29 loc) · 1.14 KB
/
290_Word-Pattern.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
class Solution {
public:
bool wordPattern(string pattern, string s) {
// Use map to map pattern and word
unordered_map<char, string> map;
// Use set to avoid multiple pattern mapping to the same word
unordered_set<string> set_appeared;
int s_idx = 0, c_idx = 0;
// Consider the boundary of pattern and s
// Mismatch happens if s_idx reachs to the end, but c_idx still there
while(c_idx < pattern.length() && s_idx < s.length()){
char c = pattern[c_idx];
int st = s_idx;
while(s_idx < s.length() && s[s_idx] != ' ') ++s_idx;
string word = s.substr(st, s_idx-st);
if(map.count(c)){
if(map[c] != word) return false;
}else{
// check word is unique, which means it's not in the value of map
if(set_appeared.count(word)) return false;
map[c] = s.substr(st, s_idx-st);
set_appeared.insert(word);
}
++s_idx;
++c_idx;
}
return s_idx == s.length()+1 && c_idx == pattern.length();
}
};