290. Word Pattern

problem

solution

option 1

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
class Solution {
public:
vector<string> split(string s){
vector<string> ret;
string cur ;
for(char c:s){
if(c==' '){
ret.push_back(cur);
cur.clear();
}
else cur+=c;
}
if(!cur.empty()) ret.push_back(cur);
return ret;
}
bool wordPattern(string pattern, string s) {
unordered_map<char,int> mpa;
unordered_map <string,int> mpb;
vector<string> str = split(s);
if(str.size()!=pattern.size()) return false;
for(int i=0;i<pattern.size() ; ++i){
char c = pattern[i];
if(mpa[c]!=mpb[str[i]]) return false;
mpa[c] = i+1;
mpb[str[i]] = i+1;
}
return true;


}
};

option 2

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
class Solution {
public:
vector<string> split(string s){
vector<string> ret;
string cur ;
for(char c:s){
if(c==' '){
ret.push_back(cur);
cur.clear();
}
else cur+=c;
}
if(!cur.empty()) ret.push_back(cur);
return ret;
}
bool wordPattern(string pattern, string s) {
unordered_map<char,string> mp;
vector<string> str = split(s);
if(str.size()!=pattern.size()) return false;
for(int i=0;i<pattern.size() ; ++i){
char c = pattern[i];
if(mp.count(c)){
if(mp[c] !=str[i]) return false;
}
// not exist in map key
else {
for(auto a:mp){
if(a.second == str[i]) return false;
}
mp[c] = str[i];
}
}
return true;


}
};

analysis

  • time complexity O(n)
  • space complexity O(n)