Different results in OJ and local


  • 0
    W

    I run this code with "abba" "1 2 2 1". OJ returns false. But in local, I use gcc-4.8, gcc-5.3 ,clang-3.7 and vs 2013 run it again with different compiler options. All return true.

       bool wordPattern(string pattern, string str) {
        	vector<const char*> p2s(26, nullptr);
        	const char * p = str.c_str();
        	const char * end = p;
        	for (char c : pattern) {
        		while (*end != ' ' && *end != '\0') ++end;
        		str[end - str.c_str()] = '\0';
        		if (p2s[c-'a'] == nullptr) {
        			p2s[c-'a'] = p;
        		} else {
        			if (strcmp(p2s[c-'a'], p) != 0) return false;
        		}
        		++end;
        		p = end;
        	}
        	return true;
        }
    

    I want to know the reason which causes this problem.
    Do I use c_str() in wrong way?


Log in to reply
 

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