-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0290_Word-Pattern.cpp
More file actions
31 lines (29 loc) · 911 Bytes
/
0290_Word-Pattern.cpp
File metadata and controls
31 lines (29 loc) · 911 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
class Solution {
public:
bool wordPattern(string pattern, string s) {
stringstream ss(s);
string tmp;
vector<string> vs;
while (ss >> tmp)
vs.emplace_back(tmp);
if (vs.size() != pattern.size())
return false;
unordered_map<char, string> m;
unordered_map<string, char> mm;
for (int i = 0; i < pattern.size(); ++i) {
int m_find = m.count(pattern[i]);
int mm_find = mm.count(vs[i]);
if (m_find <= 0 && mm_find <= 0) {
m[pattern[i]] = vs[i];
mm[vs[i]] = pattern[i];
} else if (m_find > 0 && mm_find > 0) {
if (m[pattern[i]] == vs[i] && mm[vs[i]] == pattern[i])
continue;
return false;
} else {
return false;
}
}
return true;
}
};