Simple C++ solution, why runtime error?


  • 0
    R

    For below solution, it always Runtime Error on Submission. But all custom test cases could pass.

    Submission Result: Runtime Error More Details
    Last executed input:
    "jquery"
    "jquery"

    bool wordPattern(string pattern, string str) {
            unordered_map<char,string> p2s;
            unordered_map<string,char> s2p;
    
            int start=0;
            for(char c:pattern) {
                int j=start;
                for(; j<str.length(); ++j) {
                    if(j+1<str.length() && str[j+1]==' ') break;
                }
    
                string s=string(str, start, j-start+1);
                start=j+2;
    
                if(p2s.emplace(c,s).first->second!=s || s2p.emplace(s,c).first->second!=c) {
                    return false;
                }
            }
    
            return true;
    }

Log in to reply
 

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