-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-290.cpp
More file actions
36 lines (34 loc) · 1.08 KB
/
LeetCode-290.cpp
File metadata and controls
36 lines (34 loc) · 1.08 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
/*************************************************************************
> File Name: LeetCode-290.cpp
> Author: ltw
> Mail: 3245849061@qq.com
> Created Time: Thu 21 May 2020 06:47:10 PM CST
************************************************************************/
#include <iostream>
using namespace std;
class Solution {
public:
string getNext(string &s, int &i) {
while (s[i] == ' ') ++i;
string ret = "";
while (s[i] && s[i] != ' ') ret += s[i++];
return ret;
}
bool wordPattern(string p, string s) {
string p_code[256], temp;
unordered_map<string, char> s_code;
int ind = 0;
for (int i = 0; p[i]; i++) {
temp = getNext(s, ind);
if (temp == "") return false;
if (p_code[p[i]] == "") {
if (s_code.find(temp) != s_code.end()) return false;
p_code[p[i]] = temp;
s_code[temp] = p[i];
}
if (p_code[p[i]] != temp) return false;
}
if (s[ind]) return false;
return true;
}
};