97 Interleaving String
Last updated
Was this helpful?
Last updated
Was this helpful?
Given three strings:s1,s2,s3, determine whethers3_is formed by the interleaving of_s1_and_s2.
Example
For s1 ="aabcc"
, s2 ="dbbca"
When s3 ="aadbbcbcac"
, returntrue
.
When s3 ="aadbbbaccc"
, returnfalse
.
O(n2) time or better
这题还是双序列型。[0,0]初始化为true(空串match s3的第0个也是空串),首先初始化第一列和第一行。如果s3的字母跟s2或s1的字母相同,然后前一个是match的话,我们设为true(相当于只用s1或s2来match s3)。填完两边然后填中间,如果s3的字母跟s1或s2的相等,我们判断前一个字母是否能够组成s3,如果是把当前设为true。