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 ceae027

Browse files
committedMar 27, 2025
Add solution #942
1 parent 673ab3a commit ceae027

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,026 LeetCode solutions in JavaScript
1+
# 1,027 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -751,6 +751,7 @@
751751
939|[Minimum Area Rectangle](./solutions/0939-minimum-area-rectangle.js)|Medium|
752752
940|[Distinct Subsequences II](./solutions/0940-distinct-subsequences-ii.js)|Hard|
753753
941|[Valid Mountain Array](./solutions/0941-valid-mountain-array.js)|Easy|
754+
942|[DI String Match](./solutions/0942-di-string-match.js)|Easy|
754755
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
755756
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
756757
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|

‎solutions/0942-di-string-match.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 942. DI String Match
3+
* https://leetcode.com/problems/di-string-match/
4+
* Difficulty: Easy
5+
*
6+
* A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented
7+
* as a string s of length n where:
8+
* - s[i] == 'I' if perm[i] < perm[i + 1], and
9+
* - s[i] == 'D' if perm[i] > perm[i + 1].
10+
*
11+
* Given a string s, reconstruct the permutation perm and return it. If there are multiple valid
12+
* permutations perm, return any of them.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @return {number[]}
18+
*/
19+
var diStringMatch = function(s) {
20+
const result = [];
21+
let low = 0;
22+
let high = s.length;
23+
24+
for (const char of s) {
25+
if (char === 'I') {
26+
result.push(low);
27+
low++;
28+
} else {
29+
result.push(high);
30+
high--;
31+
}
32+
}
33+
34+
result.push(low);
35+
return result;
36+
};

0 commit comments

Comments
 (0)
Please sign in to comment.