97. 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 they are divided into non-empty substrings such that:
s = s1 + s2 + ... + snt = 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 1:

Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Example 3:
Input: s1 = "", s2 = "", s3 = ""
Output: true
Constraints:
0 <= s1.length, s2.length <= 1000 <= s3.length <= 200s1,s2, ands3consist of lowercase English letters.
Follow up: Could you solve it using only O(s2.length) additional memory space?
Problem Summary #
Given three strings s1, s2, and s3, please help verify whether s3 is formed by an interleaving of s1 and s2. The definition and process of interleaving two strings s and t are as follows, where each string is split into several non-empty substrings:
- 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 means the concatenation of strings a and b.
Solution Approach #
- Use DFS or BFS to solve it by brute force. The author implemented it with DFS. Record the current comparison positions p1 and p2 in strings s1 and s2. If the position s3[p1+p2] equals s1[p1] or s2[p2], it means it can be matched, then continue moving the corresponding positions p1 and p2 backward. Because it is an interleaving string, the position to judge for matching is s3[p1+p2]. If written only this way, it will time out, because there are too many repeatedly cross-checked positions between the two strings s1 and s2. Memoized search needs to be added. A two-dimensional array like visited[i][j] can be used to record whether it has been searched. To compress space, the author encodes i and j into a one-dimensional array. i * len(s3) + j is a unique index, so this method can be used to store whether it has been searched. See the implementation below for the specific code.
Code #
package leetcode
func isInterleave(s1 string, s2 string, s3 string) bool {
if len(s1)+len(s2) != len(s3) {
return false
}
visited := make(map[int]bool)
return dfs(s1, s2, s3, 0, 0, visited)
}
func dfs(s1, s2, s3 string, p1, p2 int, visited map[int]bool) bool {
if p1+p2 == len(s3) {
return true
}
if _, ok := visited[(p1*len(s3))+p2]; ok {
return false
}
visited[(p1*len(s3))+p2] = true
var match1, match2 bool
if p1 < len(s1) && s3[p1+p2] == s1[p1] {
match1 = true
}
if p2 < len(s2) && s3[p1+p2] == s2[p2] {
match2 = true
}
if match1 && match2 {
return dfs(s1, s2, s3, p1+1, p2, visited) || dfs(s1, s2, s3, p1, p2+1, visited)
} else if match1 {
return dfs(s1, s2, s3, p1+1, p2, visited)
} else if match2 {
return dfs(s1, s2, s3, p1, p2+1, visited)
} else {
return false
}
}