C++ 0ms solution, straight but stupid.


  • 1
    S

    class Solution {
    public:

    int hashinver(string a)
    {
        if(a.compare("a")==0)
            return 0;
        if(a.compare("b")==0)
            return 1;
            if(a.compare("c")==0)
            return 2;
            if(a.compare("d")==0)
            return 3;
            if(a.compare("e")==0)
            return 4;
            if(a.compare("f")==0)
            return 5;
            if(a.compare("g")==0)
            return 6;
            if(a.compare("h")==0)
            return 7;
            if(a.compare("i")==0)
            return 8;
            if(a.compare("j")==0)
            return 9;
            if(a.compare("k")==0)
            return 10;
            if(a.compare("l")==0)
            return 11;
            if(a.compare("m")==0)
            return 12;
            if(a.compare("n")==0)
            return 13;
            if(a.compare("o")==0)
            return 14;
            if(a.compare("p")==0)
            return 15;
            if(a.compare("q")==0)
            return 16;
            if(a.compare("r")==0)
            return 17;
            if(a.compare("s")==0)
            return 18;
            if(a.compare("t")==0)
            return 19;
            if(a.compare("u")==0)
            return 20;
            if(a.compare("v")==0)
            return 21;
            if(a.compare("w")==0)
            return 22;
            if(a.compare("x")==0)
            return 23;
            if(a.compare("y")==0)
            return 24;
            if(a.compare("z")==0)
            return 25;
    
    }
    bool wordPattern(string pattern, string str) {
        vector <string> mstr;
        int fi = 0,si=0;
        for(;si<str.length();si++)
        {
            if(str.substr(si,1).compare(" ")==0)
            {
                mstr.push_back(str.substr(fi,si-fi));
                fi = si+1;
            }
        }
        mstr.push_back(str.substr(fi,si-fi+1));
        if(mstr.size()!=pattern.length())
            return false;
        if(pattern.length()==1)
            return true;
        vector <string> hash;
        for(int i=0;i<26;i++)
        {
            hash.push_back("");
        }
        
        for(int i=0 ; i < pattern.length();i++)
        {
            if(hash[hashinver(pattern.substr(i,1))].compare("")==0)
                hash[hashinver(pattern.substr(i,1))] += mstr[i];
            else
            {
                if(hash[hashinver(pattern.substr(i,1))].compare(mstr[i])!=0)
                    return false;
            }
        }
        for(int i = 0;i<25;i++)
        {
            if (hash[i].compare("")==0)
                continue;
            for(int j = i + 1;j<26;j++)
            {
                if(hash[i].compare(hash[j])==0)
                    return false;
            }
        }
        return true;
    }
    

    };


Log in to reply
 

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