Difficult:Medium
題目
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 s and t are divided into n and m non-empty substrings respectively, such that:
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 is the concatenation of strings a and b.
翻譯
給定字符串 s1、s2 和 s3,找出 s3 是否由 s1 和 s2 交織而成。
兩個字符串 s 和 t 的交錯是一種配置,其中 s 和 t 分別分為 n 和 m 個非空子字符串,使得:
s = s1 + s2 + … + sn
t = t1 + t2 + … + tm
|n - m| <= 1
交織是 s1 + t1 + s2 + t2 + s3 + t3 + … 或 t1 + s1 + t2 + s2 + t3 + s3 + …
注意:a + b 是字符串 a 和 b 的連接。
範例
Example 1:
1 | Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" |
Example 2:
1 | Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" |
Example 3:
1 | Input: s1 = "", s2 = "", s3 = "" |
解題思路
1.利用DP來解
2.obj紀錄以跑過的狀態減少時間
Solution
1 | var isInterleave = function (s1, s2, s3) { |