package com.stevesun.solutions;

public class InterleavingString {
    public boolean isInterleave(String s1, String s2, String s3) {
        // write your code here
        int m = s1.length(), n = s2.length();
        if(m+n != s3.length()) return false;
        
        boolean[][] dp = new boolean[m+1][n+1];
        
        dp[0][0] = true;
        
        for(int i = 0; i < m; i++){
            if(s1.charAt(i) == s3.charAt(i)){
                dp[i+1][0] = true;
            } 
            else {//if one char fails, that means it breaks, the rest of the chars won't matter any more.
                //Mian and I found one missing test case on Lintcode: ["b", "aabccc", "aabbbcb"]
                //if we don't break, here, Lintcode could still accept this code, but Leetcode fails it.

                break;
            }
        }
        
        for(int j = 0; j < n; j++){
            if(s2.charAt(j) == s3.charAt(j)){
                dp[0][j+1] = true;
            }
            else {
                break;
            }
        }
        
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                int k = i+j-1;
                dp[i][j] = (s1.charAt(i-1) == s3.charAt(k) && dp[i-1][j])
                || (s2.charAt(j-1) == s3.charAt(k) && dp[i][j-1]);
            }
        }
        
        return dp[m][n];
    }
}

@@ -60,16 +60,258 @@
 tring %7B%0A
+    /**Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.%0A%0A     For example,%0A     Given:%0A     s1 = %22aabcc%22,%0A     s2 = %22dbbca%22,%0A%0A     When s3 = %22aadbbcbcac%22, return true.%0A     When s3 = %22aadbbbaccc%22, return false.*/%0A
     publ

add problem description