8ms short C++ solution


  • 0
    Z
    class Solution {
    public:
        bool isInterleave(string s1, string s2, string s3) {
            if(s1.length() + s2.length() != s3. length()){
                return false;
            }
            bool a[1000][1000] = {false};
            a[0][0] = true;
            for(int i=0;i<=s2.length();i++){
                for(int j=0;j<=s1.length();j++){
                    if((i>0 && s3[i+j-1] == s2[i-1]  && a[i-1][j]) || (j>0 && s3[i+j-1] == s1[j-1] && a[i][j-1])){
                        a[i][j] = true;
                    }
                }
            }
            return a[s2.length()][s1.length()];
        }
    };

Log in to reply
 

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