DP C++ easy to follow


  • 0
    G
    class Solution {
    public:
        bool isInterleave(string s1, string s2, string s3) {
            
            if(s3.size() != s1.size() + s2.size()){
                return false;
            }
            
            vector<vector<bool> > D(s1.size() + 1,vector<bool>(s2.size() + 1,false));
            
            D[0][0] = true;
        
            for(int i = 0; i <= s1.size(); ++i){
                for(int j = 0; j <= s2.size(); ++j){
                    
                    if(i == 0 && j == 0){
                        continue;
                    }
                    
                    if(s3[i + j - 1] == s1[i - 1]){
                        D[i][j] = D[i - 1][j];
                    }
                    
                    if(D[i][j] == false && s3[i + j - 1] == s2[j - 1]){
                        D[i][j] = D[i][j - 1];
                    }
                }
            }
            
            return D[s1.size()][s2.size()];
        }
    };
    

Log in to reply
 

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