Simple and clear c++ solution


  • 0
    Y
    class Solution {
    public:
        bool wordPattern(string pattern, string str) {
            istringstream sstr(str);
            map<char,string> c2strmap;
            set<string> strset;
            string tmp;
            for(int i=0;i<pattern.size();++i)
            {
                sstr>>tmp;
                if(c2strmap.count(pattern[i]))
                 {
                     if(c2strmap[pattern[i]]!=tmp)
                     return false;
                 }
                 else
                 {
                     if(strset.count(tmp))
                        return false;
                      strset.insert(tmp);
                     c2strmap[pattern[i]]=tmp;
                 }
            }
            if(sstr>>tmp)
             return false;
            return true;
        }
    };

  • 0
    Y
    This post is deleted!

Log in to reply
 

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