-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathword_pattern.cpp
34 lines (33 loc) · 1.07 KB
/
word_pattern.cpp
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
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string str) {
unordered_map<char, string> pattern_to_str;
unordered_map<string, char> str_to_pattern;
int i = 0;
int start = 0;
for (int j = 0; j <= str.size(); ++j){
if (i >= pattern.size()) return false;
if (j == str.size() || str[j] == ' ') {
string sub = str.substr(start, j-start);
if (pattern_to_str.count(pattern[i]) == 0 && str_to_pattern.count(sub) == 0) {
pattern_to_str[pattern[i]] = sub;
str_to_pattern[sub] = pattern[i];
}else if (pattern_to_str.count(pattern[i]) != 0 && str_to_pattern.count(sub) != 0) {
if (pattern_to_str[pattern[i]] != sub || str_to_pattern[sub] != pattern[i]) {
return false;
}
}else {
return false;
}
++i;
start = j+1;
}
}
if (i != pattern.size()) return false;
return true;
}
};