My solution runtime is 0ms and O(n) space in C


  • 0
    S

    My solution runtime is 0ms ans O(n) space

    bool dp[1010];
    bool isInterleave(char* s1, char* s2, char* s3) {
        int aLen = strlen(s1), bLen = strlen(s2), cLen = strlen(s3);
        if(aLen + bLen != cLen) return false;
        dp[0] = true;
        for(int j = 1; j <= bLen; j++)
            dp[j] = dp[j - 1] && (s2[j - 1] == s3[j - 1]);
        for(int i = 1; i <= aLen; i++){
            char cur = s1[i - 1];
            dp[0] = dp[0] && (s1[i - 1] == s3[i - 1]);
            for(int j = 1; j <= bLen; j++)
                dp[j] = (dp[j] && cur == s3[i + j - 1]) || (dp[j - 1] && s2[j - 1] == s3[i + j - 1]);
        }
        return dp[bLen];
    }

Log in to reply
 

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