-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
48 lines (45 loc) · 1.19 KB
/
solution.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
class Solution
{
public:
bool wordPattern(string pattern, string str)
{
vector<string> strList;
string temp;
for(int i=0;i<str.size();i++)
{
if(str[i] != ' ')
temp += str[i];
else
{
strList.push_back(temp);
temp = "";
}
}
strList.push_back(temp);
if(pattern.size() != strList.size())
return false;
unordered_map<char,string> table1;
for(int i=0;i<pattern.size();i++)
{
if(table1.find(pattern[i]) == table1.end())
table1[pattern[i]] = strList[i];
else
{
if(table1[pattern[i]] != strList[i])
return false;
}
}
unordered_map<string,char> table2;
for(int i=0;i<strList.size();i++)
{
if(table2.find(strList[i]) == table2.end())
table2[strList[i]] = pattern[i];
else
{
if(table2[strList[i]] != pattern[i])
return false;
}
}
return true;
}
};