forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0942-di-string-match.js
More file actions
36 lines (33 loc) · 808 Bytes
/
0942-di-string-match.js
File metadata and controls
36 lines (33 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 942. DI String Match
* https://leetcode.com/problems/di-string-match/
* Difficulty: Easy
*
* A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented
* as a string s of length n where:
* - s[i] == 'I' if perm[i] < perm[i + 1], and
* - s[i] == 'D' if perm[i] > perm[i + 1].
*
* Given a string s, reconstruct the permutation perm and return it. If there are multiple valid
* permutations perm, return any of them.
*/
/**
* @param {string} s
* @return {number[]}
*/
var diStringMatch = function(s) {
const result = [];
let low = 0;
let high = s.length;
for (const char of s) {
if (char === 'I') {
result.push(low);
low++;
} else {
result.push(high);
high--;
}
}
result.push(low);
return result;
};