-
Notifications
You must be signed in to change notification settings - Fork 9
/
7-september.txt
36 lines (36 loc) · 963 Bytes
/
7-september.txt
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
class Solution {
public:
bool wordPattern(string pattern, string str) {
vector<string> v;
string temp;
for(int i=0;i<str.length();i++)
{
if(str[i]!=' ')
temp+=str[i];
else
v.push_back(temp),temp="";
}
if(temp.length())
v.push_back(temp);
if(v.size()!=pattern.length())
return false;
set<char> p;
set<string> s;
map<char,string> mp;
for(int i=0;i<pattern.length();i++)
{
if(!(s.find(v[i])==s.end() ^ p.find(pattern[i])==p.end()))
s.insert(v[i]),p.insert(pattern[i]);
else
return false;
if(mp.find(pattern[i])==mp.end())
mp[pattern[i]]=v[i];
else
{
if(mp[pattern[i]]!=v[i])
return false;
}
}
return true;
}
};