Why wrong answer in the situation { "a" "" "c"}?


  • -4
    H

    I run this code on XCode, and got an answer of FALSE, but LeetCode says my answer is true. Could anyone tell me the reason?

    bool isInterleave(string s1, string s2, string s3) {
        if(s1.length()+s2.length() != s3.length()){
            return false;
        }
        int dp_table[1000][1000];
        dp_table[0][0] = true;
        for(int i = 1; i<=s1.length(); i++){
            if(s1[i-1] == s3[i-1]){
                dp_table[i][0] = true;
            }else{
                break;
            }
        }
    
        for(int j = 1; j<=s2.length(); j++){
            if(s2[j-1] == s3[j-1]){
                dp_table[0][j] = true;
            }else{
                break;
            }
        }
        
        for(int i = 1; i<=s1.length(); i++){
            for(int j = 1; j<=s2.length(); j++){
                if(s1[i-1] == s3[i+j-1] && dp_table[i-1][j]){
                    dp_table[i][j] = true;
                }else if(s2[j-1] == s3[i+j-1] && dp_table[i][j-1]){
                    dp_table[i][j] = true;
                }else{
                    dp_table[i][j] = false;
                }
            }
        }
    
        return dp_table[s1.length()][s2.length()];
    }

  • 0

    Rather than relying on the debugger, did you try going through the code yourself? There could be some bug in your code (for example, index out of range access) which could lead to undefined behavior -- and hence, different result as you've observed.


Log in to reply
 

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