题目描述:
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.
那么状态式定义为:
f[ k ] [ i ] [ j ] = ( f [ k-1 ] [i ] [ j - 1] && s2[ j-1] == s3 [ k -1] // s2的第j-1个字符与s3的第k-1个字符匹配。
或者是 = ( f [ k-1 ] [ i-1 ] [ j ] && s1 [ i-1] == s3 [ k -1] // s1的第i-1个字符与s3的第k-1个字符匹配。
两者只要有一个满足条件即可。
代码如下:
Version 1: 时间复杂度 O(n^3),空间复杂度 O(n^3).Time : ~200ms
Version 2: 时间复杂度 O(n^2),空间复杂度 O(n^2).Time :~ 4ms
Version 3: 时间复杂度 O(n^2),空间复杂度 O(n). Time :~ 4ms
使用滚动数组来解决:
leetcode - interleaving string,布布扣,bubuko.com
leetcode - interleaving string
原文:http://blog.csdn.net/xuqingict/article/details/37612171