-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathWord_Pattern.cpp
More file actions
28 lines (28 loc) · 838 Bytes
/
Word_Pattern.cpp
File metadata and controls
28 lines (28 loc) · 838 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
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map <char, string> Map;
unordered_map<string, char> rMap;
vector<string> words;
string word;
stringstream SS(str);
while(getline(SS, word, ' ')) {
words.push_back(word);
}
if(pattern.length() != words.size()) {
return false;
}
for(int i = 0; i < pattern.length(); ++i) {
if(Map.find(pattern[i]) != Map.end()) {
if(Map[pattern[i]] != words[i]) {
return false;
}
} else if(rMap.find(words[i]) != rMap.end()) {
return false;
}
Map[pattern[i]] = words[i];
rMap[words[i]] = pattern[i];
}
return true;
}
};