-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path290.单词规律.cpp
62 lines (59 loc) · 1.94 KB
/
290.单词规律.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=290 lang=cpp
*
* [290] 单词规律
*/
// @lc code=start
#include <vector>
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<string, char> map;
unordered_map<char, string> reversed_map;
string tmp = "";
int j = 0;
for(int i = 0; i < s.size(); ++i){
char cur = s[i];
cout << "cur: " << cur << endl;
if(cur == ' ' || i == s.size() - 1){
if(i == s.size() - 1)
tmp += string(1, cur);
unordered_map<string, char>::const_iterator got = map.find(tmp);
cout << "j: " << j << endl;
if(got == map.end()){
map[tmp] = pattern[j];
}else{
if(map[tmp] != pattern[j]){
cout << "tmp: " << tmp << endl;
cout << "pattern char: " << pattern[j] << " " << "j: " << j << endl;
cout << "not" << endl;
return false;
}
}
unordered_map<char, string>::const_iterator got2 = reversed_map.find(pattern[j]);
if(got2 == reversed_map.end()){
reversed_map[pattern[j]] = tmp;
}else{
if(reversed_map[pattern[j]] != tmp){
cout << "tmp: " << tmp << endl;
cout << "pattern char: " << pattern[j] << " " << "j: " << j << endl;
cout << "not" << endl;
return false;
}
}
tmp = "";
j ++;
}else{
tmp += string(1, cur);
}
}
if(j != pattern.size())
return false;
return true;
}
};
// @lc code=end