290. Word Pattern 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 112345678910111213141516171819202122232425262728293031class 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 212345678910111213141516171819202122232425262728293031323334353637class 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)