问题描述:
字符串交织,判断字符串str3, 能否由str1, str2交织而成。
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.
问题分析:
定义:
str1[1, m] 字符串str1 长度m 开始下标1
str2[1, n] 字符串str2 长度m 开始下标1
str3[1, l] 字符串str3 长度m 开始下标1
f[m][n] 长度为m和n的两个字符串能否交织成字符串str3
算法:
if (m + n != l) return false;
f[m][n] = (f[m-1][n] && str1[m] == str3[m+n]) || (f[m][n-1] && str2[n-1] == str3[m+n])
编程的时候需要处理好边界问题