97. Interleaving String
# Hard 太难了
How to define interLeave[i][j]
is the key. Two-sequence DP problem. interLeave[i][j] means s1[:i]
and s2[:j]
can completely match string of s3[:i+j]
. bool value.
如何实现 interLeave[i][j]
,如何做这道题,取决于如何定义interLeave
这种有循环直奔结果的数组,并且正确的初始化第一个元素
Solution:
Initilize
interLeave[len(s1)+1][len(s2)+1]
,interLeave[i][0]
andinterLeave[0][j]
Two cases can match:
s1[i] = s3[i+j]
andinterLeave[i-1][j]=True
s2[j] = s3[i+j]
andinterLeave[i][j-1]=True
Then
interLeave[i][j] = True
Don't forget edge case, which can save a lot of time.
Last updated
Was this helpful?