0ms C++ solution


  • 0
    Z
    class Solution {
    public:
        bool wordPattern(string pattern, string str) {
            vector<string> tmp;
            string s="";
            for(int i=0;i<str.size();i++){
                if(str[i]!=' ') s+=str[i];
                else{
                    tmp.push_back(s);
                    s="";
                }
            }
            tmp.push_back(s);
            if(pattern.size()!=tmp.size()) return false;
            unordered_map<char,int> p;
            unordered_map<string,int> m;
            for(int i=0;i<tmp.size();i++){
                if(p[pattern[i]]!=m[tmp[i]]) return false;
                p[pattern[i]]=i+1;
                m[tmp[i]]=i+1;
            }
            return true;
        }
    };

  • 0
    A

    At least format it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.