Skip to content

Commit 97c16f7

Browse files
committed
Time: 4 ms (32.33%), Space: 6.5 MB (19.87%) - LeetHub
1 parent 3de4e29 commit 97c16f7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
};

0 commit comments

Comments
 (0)