-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path290.cpp
More file actions
32 lines (32 loc) · 990 Bytes
/
290.cpp
File metadata and controls
32 lines (32 loc) · 990 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
32
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char, string> c2w;
unordered_map<string, char> w2c;
int n = pattern.size();
int index = 0;
string temp;
stringstream ss(s);
while (getline(ss, temp, ' ')) {
if (index == n) return false;
int c = pattern[index];
if (c2w.count(c) == 0 && w2c.count(temp) == 0) {
c2w[c] = temp;
w2c[temp] = c;
}
else if (c2w.count(c) != 0 && w2c.count(temp) == 0) {
return false;
}
else if (c2w.count(c) == 0 && w2c.count(temp) != 0) {
return false;
}
else {
string targetWord = c2w[c];
int targetC = w2c[temp];
if (targetWord != temp || targetC != c) return false;
}
index++;
}
return index == n;
}
};