1
class Solution {
2
public:
3
unordered_set<string> processed_words;
4
string m[26]; // char to string mapping
5

6
bool crunch_next_word(char c, string word) {
7
int idx = c - 'a';
8
if (m[idx].empty() && processed_words.count(word) == 0) {
9
m[idx] = word;
10
processed_words.insert(word);
11
return true;
12
} else if (m[idx] == word)
13
return true;
14
else
15
return false;
16
}
17

18
bool wordPattern(string pattern, string s) {
19
int count = 0;
20

21
int start = 0;
22
int end = s.find(' ');
23
while (end != -1) {
24
string word = s.substr(start, end - start);
25
char c = pattern[count];
26
if (!crunch_next_word(c, word)) return false;
27

28
start = end + 1;
29
end = s.find(' ', start);
30
count++;
31
if (count == pattern.length()) return false;
32
}
33
if (count != pattern.length() - 1) return false;
34
string word = s.substr(start, end - start);
35
char c = pattern[count];
36
if (!crunch_next_word(c, word)) return false;
37
return true;
38
}
39
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0