0ms c++ solution without map


  • 0
    S

    At first,I want to use hash_map,failed;then I do it without map like this:

    bool wordPattern(string pattern, string str) {

        vector<string> responstr(26,"");
        string* strarray=new string[26];
        int startpos=0;
        int strnum=0;
        int strcount=0;
        for(int i=0;i<pattern.size();++i)
        {
            int endpos=str.find(' ',startpos);
            ++strnum;
            if (string::npos == endpos)
            {
                if(strnum!=pattern.size())
                    return false;
                endpos = str.size();
            }
                    
    		int num=endpos-startpos;
            string curstr=str.substr(startpos,num);
            startpos=endpos+1;
            char curchar=pattern[i];
            if(""==responstr[curchar-'a'])
            {
                responstr[curchar-'a']=curstr;
                for(int j=0;j<strcount;++j)
                {
                    if(curstr==strarray[j])
                    return false;
                }
                strarray[strcount]=curstr;
                ++strcount;
            }
                    
            else
            {
                if(curstr!=responstr[curchar-'a'])
                return false;
            }
    
                    
                    
        }
        if(startpos!=(str.size()+1))
            return false;
        return true;
    }

Log in to reply
 

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