File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool wordPattern (string pattern, string s) {
4+
5+ int count = 0 ;
6+ stringstream ss (s);
7+ vector<string> words;
8+ string word;
9+
10+ int n = pattern.size ();
11+
12+ while (ss >> word)
13+ words.push_back (word);
14+
15+ if (n != words.size ())
16+ return false ;
17+
18+ unordered_map<char ,string> mp1;
19+ unordered_map<string,bool > mp2;
20+
21+ for (int i = 0 ; i<n; ++i)
22+ {
23+ char ch = pattern[i];
24+
25+ if (mp1.find (ch) == mp1.end ())
26+ {
27+ if (mp2.find (words[i]) == mp2.end ())
28+ {
29+ mp1[ch] = words[i];
30+ mp2[words[i]] = true ;
31+ }
32+ else
33+ return false ;
34+ }
35+ else
36+ {
37+ string check = mp1[ch];
38+ if (check != words[i])
39+ return false ;
40+ }
41+ }
42+
43+ return true ;
44+ }
45+ };
You can’t perform that action at this time.
0 commit comments