Algorithm


Problem Nmae: 97. Interleaving String

 

Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.

An interleaving of two strings s and t is a configuration where s and t are divided into n and m

respectively, such that:

 

  • s = s1 + s2 + ... + sn
  • t = t1 + t2 + ... + tm
  • |n - m| <= 1
  • The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...

Note: a + b is the concatenation of strings a and b.

Example 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Explanation: One way to obtain s3 is:
Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
Since s3 can be obtained by interleaving s1 and s2, we return true.

Example 2:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.

Example 3:

Input: s1 = "", s2 = "", s3 = ""
Output: true

Constraints:

  • 0 <= s1.length, s2.length <= 100
  • 0 <= s3.length <= 200
  • s1, s2, and s3 consist of lowercase English letters.
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean isInterleave(String s1, String s2, String s3) {
    if (s1.length() + s2.length() != s3.length()) {
      return false;
    }
    Boolean[][] dp = new Boolean[s1.length()][s2.length()];
    return isInterleaveHelper(s1, 0, s2, 0, s3, 0, dp);
  }
  
  private boolean isInterleaveHelper(String s1, int i1, String s2, int i2, String s3, int i3, Boolean[][] dp) {
    if (i1 == s1.length()) {
      return s2.substring(i2).equals(s3.substring(i3));
    }
    if (i2 == s2.length()) {
      return s1.substring(i1).equals(s3.substring(i3));
    }
    if (dp[i1][i2] != null) {
      return dp[i1][i2]; 
    }
    dp[i1][i2] = (s3.charAt(i3) == s1.charAt(i1) && isInterleaveHelper(s1, i1 + 1, s2, i2, s3, i3 + 1, dp)) || 
                 (s3.charAt(i3) == s2.charAt(i2) && isInterleaveHelper(s1, i1, s2, i2 + 1, s3, i3 + 1, dp));
    return dp[i1][i2];
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"

Output

x
+
cmd
true

#2 Code Example with Javascript Programming

Code - Javascript Programming


const isInterleave = function(s1, s2, s3) {
  const len3 = s3.length
  const len1 = s1.length
  const len2 = s2.length
  if(len1 + len2 !== len3) return false
  
  const dp = Array.from({length: len1 + 1}, () => new Array(len2 + 1).fill(false))
  for(let i = 0; i <= len1; i++) {
    for(let j = 0; j  < = len2; j++) {
      if(i === 0 && j === 0) {
        dp[i][j] = true
      } else if(i === 0) {
        dp[i][j] = dp[i][j - 1] && s2[j - 1] === s3[i + j - 1]   
      } else if(j === 0) {
        dp[i][j] = dp[i - 1][j] && s1[i - 1] === s3[i + j - 1]      
      } else {
        dp[i][j] = (dp[i - 1][j] && s1[i - 1] === s3[i + j - 1]) || (dp[i][j - 1] && s2[j - 1] === s3[i + j - 1]>
      }
    }
  }
  
  return dp[len1][len2]
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"

Output

x
+
cmd
false

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isInterleave(self, s1, s2, s3):
        def dfs(i, j, k):
            if (i, j, k) not in memo:
                memo[(i, j, k)] = k>=l3 or (i
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "", s2 = "", s3 = ""

Output

x
+
cmd
true

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _097_InterleavingString
    {
        public bool IsInterleave(string s1, string s2, string s3)
        {
            if (s1.Length + s2.Length != s3.Length) return false;

            var dp = new bool[s1.Length + 1, s2.Length + 1];
            for (int i = 0; i  < = s1.Length; i++)
            {
                for (int j = 0; j  < = s2.Length; j++)
                {
                    if (i == 0 && j == 0) dp[i, j] = true;
                    else if (i == 0) dp[i, j] = dp[i, j - 1] && s2[j - 1] == s3[i + j - 1];
                    else if (j == 0) dp[i, j] = dp[i - 1, j] && s1[i - 1] == s3[i + j - 1];
                    else
                        dp[i, j] = (dp[i, j - 1] && s2[j - 1] == s3[i + j - 1]) || (dp[i - 1, j] && s1[i - 1] == s3[i + j - 1]);
                }
            }

            return dp[s1.Length, s2.Length];
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"

Output

x
+
cmd
true
Advertisements

Demonstration


Previous
#96 LeetcodeUnique Binary Search Trees Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#98 Leetcode Validate Binary Search Tree Solution in C, C++, Java, JavaScript, Python, C# Leetcode