LC097 - Interleaving String
Problem
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
substrings respectively, such that:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
The interleaving is
s1 + t1 + s2 + t2 + s3 + t3 + ...
ort1 + s1 + t2 + s2 + t3 + s3 + ...
Note: a + b
is the concatenation of strings a
and b
.
Example
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.
Solution
Cached DFS
We find whether or not it is possible to merge s1
and s2
into s3
, with the substrings in original order. We use three pointers, one on each string, incrementing when a match is found between s1/s2
and s3
, alternating between s1
and s2
when a match is not found. When it is possible to increment the pointer at both s1
and s2
, there exists a branch in the decision tree, and we must then backtrack. Caching is used to reduce the effective depth of the tree from to , at the cost of extra space used for the cache.
2D Dynamic Programming
The 2D dynamic programming approach takes about the same time and memory as the cached DFS approach.
Last updated