-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathword-pattern.cpp
More file actions
30 lines (26 loc) · 815 Bytes
/
Copy pathword-pattern.cpp
File metadata and controls
30 lines (26 loc) · 815 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
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char, string> char_map;
unordered_map<string, char> word_map;
stringstream s(str);
string word;
int n = pattern.size(), i = 0;
while(s >> word){
if(i == n)
return false;
char c = pattern[i];
if(char_map.count(c) != word_map.count(word))
return false;
if(char_map.count(c)){
if( (word_map[word] != c) || (char_map[c] != word))
return false;
} else {
char_map.insert({c, word});
word_map.insert({word, c});
}
i++;
}
return i == n;
}
};