The code giving correct answer o "Run Code" but wrong answer for same test case on submitting


  • 0
    G
    class Solution {
    public:
        bool wordPattern(string pattern, string str) {
            map<char,string> bij;
            map<string,char> sur;
            int j=0;
            if(pattern.size()==0){
                if(str.size()!=0)
                    return false;
                else return true;
            }
            for(int i=0;i<pattern.size();i++){
                string word;
                while(str[j]!=' '&&str[j]!='\0'){
                    word.push_back(str[j]);
                    j++;
                }
                j++;
                if(word.size()==0)
                    return false;
                if(bij.find(pattern[i])==bij.end()){
                    if(sur.find(word)!=sur.end())
                        return false;
                    else{
                        bij[pattern[i]]=word;
                        sur[word]=pattern[i];
                    }
                }
                if(bij[pattern[i]]!=word||sur[word]!=pattern[i])
                    return false;
            }
            j--;
            if(str[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.