Skip to content

Bug Report for interleaving-string #5223

@reubencapio

Description

@reubencapio

Bug Report for https://neetcode.io/problems/interleaving-string

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

This solution passes in your test:

class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
//can be solved with recursion using dfs and caching
vector<vector> cache(s1.size() + 1, vector(s2.size() + 1, -1));
return dfs(0,0,0,s1,s2,s3,cache);
}

bool dfs(int i, int j, int k, string& s1, string& s2, string& s3, vector<vector<int>>& cache){
    //success criteria
    if(s3.size() == k){
        return s1.size() == i && s2.size() == j;
    }

    //cache
    if(cache[i][j] != -1) return cache[i][j];

    bool res = false;
    //other conditions
    if(i < s1.size() && s1[i] == s3[k]){
        res = dfs(i+1, j, k+1, s1,s2,s3, cache);
    }

    if(j < s2.size() && s2[j] == s3[k]){
        res = dfs(i, j+1, k+1, s1,s2,s3, cache);
    }

    cache[i][j] = res;
    return cache[i][j];
}

};

but it fails with this test case: s1 = "aabc", s2 = "abad", s3 = "aabcabad"
the line if(j < s2.size() && s2[j] == s3[k]){ should be if(!res && j < s2.size() && s2[j] == s3[k]){

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions