-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordPattern.cpp
86 lines (82 loc) · 2.51 KB
/
wordPattern.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
*/
//my Solution
class Solution {
public:
bool wordPattern(string pattern, string str) {
if (pattern == "" || str == "")
return false;
vector<string> vs;
for (int i = 0, j = 0; i != str.length(); i++)
{
if (str[i] == ' ')
{
vs.push_back(str.substr(j, i - j));
j = i + 1;
}
if (i == str.length() - 1)
{
vs.push_back(str.substr(j, i - j + 1));
}
}
if (pattern.length() != vs.size())
return false;
unordered_map<string, string> umcs1;
unordered_map<string, string> umcs2;
for (int i = 0; i != pattern.length(); i++)
{
string s = pattern.substr(i, 1);
if (umcs1[s] == ""&&umcs2[vs[i]] == "")
{
umcs1[s] = vs[i];
umcs2[vs[i]] = s;
}
else{
if (umcs1[s] != vs[i])
return false;
}
}
return true;
}
};
//better Solution
class Solution {
public:
bool wordPattern(string pattern, string str) {
map<char, int> p2i;
map<string, int> w2i;
istringstream in(str);
int i = 0, n = pattern.size();
for (string word; in >> word; ++i) {
if (i == n || p2i[pattern[i]] != w2i[word])
return false;
p2i[pattern[i]] = w2i[word] = i + 1;
}
return i == n;
}
};
//better Solution
class Solution {
public:
bool wordPattern(string pattern, string str) {
int p2i[26] {};
unordered_map<string, int> w2i;
istringstream in(str);
int i = 0, n = pattern.size();
for (string word; in >> word; ++i) {
if (i==n||p2i[pattern[i] - 'a'] != w2i[word])
return false;
p2i[pattern[i] - 'a'] = w2i[word] = i + 1;
}
return i == n;
}
};