forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_array_n.cpp
49 lines (44 loc) · 1.12 KB
/
AC_array_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_map_nlogn.cpp
* Create Date: 2015-10-24 22:32:22
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool wordPattern(string pattern, string str) {
string mp[26];
int i = 0;
for (auto c : pattern) {
string word = "";
// go pass space
while (str[i] != '\0' && !isalpha(str[i])) {
++i;
}
// get word
while (str[i] != '\0' && isalpha(str[i])) {
word += str[i++];
}
if (word == "")
return false;
if (mp[c - 'a'] == "") {
for (int i = 0 ; i < 26; ++i)
if (mp[i] == word)
return false;
mp[c - 'a'] = word;
} else {
if (mp[c - 'a'] != word)
return false;
}
}
return str[i] == '\0';
}
};
int main() {
Solution s;
cout << s.wordPattern("abba", "dog cat cat dog");
return 0;
}