File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,026 LeetCode solutions in JavaScript
1
+ # 1,027 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
751
751
939|[ Minimum Area Rectangle] ( ./solutions/0939-minimum-area-rectangle.js ) |Medium|
752
752
940|[ Distinct Subsequences II] ( ./solutions/0940-distinct-subsequences-ii.js ) |Hard|
753
753
941|[ Valid Mountain Array] ( ./solutions/0941-valid-mountain-array.js ) |Easy|
754
+ 942|[ DI String Match] ( ./solutions/0942-di-string-match.js ) |Easy|
754
755
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
755
756
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
756
757
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments