-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path290-Word-Pattern.cpp
More file actions
43 lines (37 loc) · 1.08 KB
/
290-Word-Pattern.cpp
File metadata and controls
43 lines (37 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
37
38
39
40
41
42
43
class Solution {
public:
bool wordPattern(string pattern, string s)
{
map<string, char>m;
map<string, bool>vis;
map<char,string>mm;
for( char i = 'a'; i <= 'z'; i++ )
mm[i] = \\;
string word = \\;
int n = s.size(), j = 0, words = 0;
for( int i = 0; i < n; i++ )
{
if( s[i] == ' ' )
{
words++;
if( vis[word] && m[word] != pattern[j] )
return false;
if( mm[pattern[j]] != word && mm[pattern[j]] != \\ )
return false;
vis[word] = 1;
m[word] = pattern[j];
mm[pattern[j]] = word;
word = \\, ++j;
}
else
word += s[i];
}
if( vis[word] && m[word] != pattern[j] )
return false;
if( mm[pattern[j]] != word && mm[pattern[j]] != \\ )
return false;
if( pattern.size() - 1 != words )
return false;
return true;
}
};