-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path290. Word Pattern.cpp
More file actions
40 lines (31 loc) · 1.11 KB
/
290. Word Pattern.cpp
File metadata and controls
40 lines (31 loc) · 1.11 KB
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
38
39
40
class Solution {
public:
bool wordPattern(string pattern, string str) {
int len = pattern.size();
if (len == 0 && str.size() == 0) return true;
if ((len == 0 && str.size() != 0) || (len != 0 && str.size() == 0)) return false;
int pos_p = 0;
int pos_s = 0;
unordered_map<char, string> mp;
while (pos_p < len) {
char ch = pattern[pos_p++];
string temp = "";
while (pos_s < str.size() && str[pos_s] != ' ') {
temp += str[pos_s++];
}
pos_s++;
if (temp.size() == 0) return false;
if (mp.find(ch) == mp.end()) {
for (auto & itr : mp) {
if (itr.second == temp) return false;
}
mp.insert(pair<char, string>(ch, temp));
}
else {
if (mp[ch] != temp) return false;
}
}
if (pos_p == len && pos_s >= str.size()) return true;
return false;
}
};