my post


  • 0
    D

    ···
    class Solution {
    public:
    bool wordPattern(string pattern, string str) {
    unordered_map<char,string> hash;
    int count=1;
    for(char &c:str){
    if(c==' ')count++;
    }
    if(count!=pattern.size())return false;
    int flag=0;
    int space=count-1;
    for(int i=0;i<count;i++){
    int tmp=flag;
    while(str[flag+i]!=' ')flag++;
    // cout<<flag<<endl;
    // cout<<"tmp"<<tmp<<endl;
    if(space==0)flag=count-i-1;
    space--;
    if(hash.find(pattern[i])==hash.end()){
    //string strtmp=str.substr[tmp+i,flag+i]
    hash[pattern[i]]=str.substr(tmp+i,flag-tmp);
    //cout<<str.substr(int(tmp+i),int(flag+i))<<endl;
    }
    //cout<<str.substr(tmp+i,flag+i)<<endl;
    // cout<<str<<endl;
    if(hash[pattern[i]]!=str.substr(tmp+i,flag-tmp))return false;
    }
    return true;
    }
    };
    ···


Log in to reply
 

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