File tree 1 file changed +23
-0
lines changed
1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode-cn.com/problems/interleaving-string/
3
+ * 97. 交错字符串
4
+ * Hard
5
+ * @param {string } s1
6
+ * @param {string } s2
7
+ * @param {string } s3
8
+ * @return {boolean }
9
+ */
10
+ var isInterleave = function ( s1 , s2 , s3 ) {
11
+ if ( s1 . length + s2 . length !== s3 . length ) return false
12
+ const len1 = s1 . length
13
+ const len2 = s2 . length
14
+ const dp = [ ...new Array ( len1 + 1 ) ] . map ( ( ) => Array ( len2 + 1 ) . fill ( 0 ) )
15
+ dp [ 0 ] [ 0 ] = 1
16
+ for ( let i = 1 ; i <= len1 && s1 [ i - 1 ] === s3 [ i - 1 ] ; i ++ ) dp [ i ] [ 0 ] = 1
17
+ for ( let i = 1 ; i <= len2 && s2 [ i - 1 ] === s3 [ i - 1 ] ; i ++ ) dp [ 0 ] [ i ] = 1
18
+ for ( let i = 1 ; i <= len1 ; i ++ ) {
19
+ for ( let j = 1 ; j <= len2 ; j ++ )
20
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j ] && s1 [ i - 1 ] === s3 [ i + j - 1 ] || dp [ i ] [ j - 1 ] && s2 [ j - 1 ] === s3 [ i + j - 1 ]
21
+ }
22
+ return dp [ len1 ] [ len2 ]
23
+ } ;
You can’t perform that action at this time.
0 commit comments