Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ef337ed

Browse files
committedJan 23, 2025
Add solution #97
1 parent 5a18fd5 commit ef337ed

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
94|[Binary Tree Inorder Traversal](./0094-binary-tree-inorder-traversal.js)|Easy|
100100
95|[Unique Binary Search Trees II](./0095-unique-binary-search-trees-ii.js)|Medium|
101101
96|[Unique Binary Search Trees](./0096-unique-binary-search-trees.js)|Medium|
102+
97|[Interleaving String](./0097-interleaving-string.js)|Medium|
102103
98|[Validate Binary Search Tree](./0098-validate-binary-search-tree.js)|Medium|
103104
99|[Recover Binary Search Tree](./0099-recover-binary-search-tree.js)|Medium|
104105
100|[Same Tree](./0100-same-tree.js)|Easy|

‎solutions/0097-interleaving-string.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 97. Interleaving String
3+
* https://leetcode.com/problems/interleaving-string/
4+
* Difficulty: Medium
5+
*
6+
* Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
7+
*
8+
* An interleaving of two strings s and t is a configuration where s and t are divided into
9+
* n and m substrings respectively, such that:
10+
* - s = s1 + s2 + ... + sn
11+
* - t = t1 + t2 + ... + tm
12+
* - |n - m| <= 1
13+
* - The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
14+
*
15+
* Note: a + b is the concatenation of strings a and b.
16+
*/
17+
18+
/**
19+
* @param {string} s1
20+
* @param {string} s2
21+
* @param {string} s3
22+
* @return {boolean}
23+
*/
24+
var isInterleave = function(s1, s2, s3) {
25+
const values = new Array(s1.length + 1).fill().map(() => []);
26+
27+
if (s1.length + s2.length !== s3.length) {
28+
return false;
29+
}
30+
31+
for (let i = 0; i <= s1.length; i++) {
32+
for (let j = 0; j <= s2.length; j++) {
33+
values[i][j] = i && values[i - 1][j] && s3[i + j - 1] === s1[i - 1]
34+
|| j && values[i][j - 1] && s3[i + j - 1] === s2[j - 1] || !i && !j;
35+
}
36+
}
37+
38+
return Boolean(values.pop().pop());
39+
};

0 commit comments

Comments
 (0)
Please sign in to comment.