-
Notifications
You must be signed in to change notification settings - Fork 0
290_WordPattern
a920604a edited this page Apr 14, 2023
·
1 revision
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;
}
};
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;
}
};
- time complexity
O(n)
- space complexity
O(n)
footer