Problem Link:
http://oj.leetcode.com/problems/interleaving-string/
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc"
,
s2 = "dbbca"
,
When s3 = "aadbbcbcac"
, return true.
When s3 = "aadbbbaccc"
, return false.
This problem can solved by using DFS on a binary tree. The tree node has three integers (p1,p2,p3), which denotes that s3[0..p3-1] is the interleaving of s1[0..p1-1] and s2[0..p2-1]. For each node, there are following choices:
- If p3 == len(s3), it follows that s3 is the interleaving of s1 and s2 (we need assume that len(s3) = len(s1)+len(s2)), return True
- If p1 < len(s1) and s1[p1] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1] and s2[0..p2-1], so we can go deeper to the node (p1+1, p2, p3+1);
- If p2 < len(s2) and s2[p2] == s3[p3], then s3[0..p3] could be the interleaving of s1[0..p1-1] and s2[0..p2], so we can go deeper to the node (p1, p2+1, p3+1);
- Otherwise, we cannot go deeper and this path will be terminated.
If we check all possible paths using DFS, and there is no successful path, we would return False and terminate the program. The python code should be as follows.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# DFS
q = []
q.append( (0,0,0) ) # checked length for s1, s2, s3
while q:
l1, l2, l3 = q.pop()
# If checked length of s3 equals to length of s3, then return True
if l3 == n3:
return True
# If availabe char in s1:
if l1 < n1 and s1[l1] == s3[l3]:
q.append((l1+1,l2,l3+1))
# If available char in s2:
if l2 < n2 and s2[l2] == s3[l3]:
q.append((l1,l2+1,l3+1))
return False
I used build-in structure list implementing the Queue structure. However, the leetcode judging system returns timeout for this DFS implementation. I think it will be worse if you use recursive function instead of queue to carry out the DFS.
So I have to solve it in a more efficient way, I choose DP. I define a boolean 2D array A[0..n1][0..n2] to denote if s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. The recursive formula for this DP solution is:
A[0][0] = True, since "" is the interleaving of "" and ""
A[0][j] = True, if s2[0:j] == s3[0:j], which means s3 is the interleaving of "" and s2 if s2 == s3
A[i][0] = True, if s1[0:i] == s3[0:i], which means s3 is the interleaving of s1 and "" if s1 == s3
A[i][j] = True if 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]; or 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1], for 0<i<=n1, 0<j<=n2
A[][] is (n1+1)*(n2+1) array where A[i][j] means s3[0:i+j] is the interleaving of s1[0:i] and s2[0:j]. We can fill the A-table in a bottom-up way and just return A[n1][n2] to see if s3 is the interleaving of s1 and s2. The python code is as follows, which accepted by the leetcode OJ system.
class Solution:
# @return a boolean
def isInterleave(self, s1, s2, s3):
# A[i][j] means s1[0:i] and s2[0:j] is interleaves of s3[0:i+j]
# A[0][0] = True, since "" and "" are interleaves of ""
# A[0][j] = True, if s2[0:j] == s3[0:j]
# A[i][0] = True, if s1[0:i] == s3[0:j]
# A[i][j] = True, if any of following is true:
# 1) A[i-1][j] = True and s1[i-1] == s3[i+j-1]
# 2) A[i][j-1] = True and s2[j-1] == s3[i+j-1]
n1 = len(s1)
n2 = len(s2)
n3 = len(s3)
if n1 + n2 != n3:
return False
# Initialization
A = []
for _ in xrange(n1+1):
A.append([False]*(n2+1))
# Boundary conditions
A[0][0] = True
for j in xrange(n2):
if s2[j] == s3[j]:
A[0][j+1] = True
for i in xrange(n1):
if s1[i] == s3[i]:
A[i+1][0] = True
# Fill the table
for x in xrange(n1):
for y in xrange(n2):
i = x+1
j = y+1
if (A[i-1][j] and s1[i-1] == s3[i+j-1]) or (A[i][j-1] and s2[j-1] == s3[i+j-1]):
A[i][j] = True
return A[n1][n2]