File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 2144
2144
3034|[ Number of Subarrays That Match a Pattern I] ( ./solutions/3034-number-of-subarrays-that-match-a-pattern-i.js ) |Medium|
2145
2145
3035|[ Maximum Palindromes After Operations] ( ./solutions/3035-maximum-palindromes-after-operations.js ) |Medium|
2146
2146
3038|[ Maximum Number of Operations With the Same Score I] ( ./solutions/3038-maximum-number-of-operations-with-the-same-score-i.js ) |Easy|
2147
+ 3039|[ Apply Operations to Make String Empty] ( ./solutions/3039-apply-operations-to-make-string-empty.js ) |Medium|
2147
2148
3042|[ Count Prefix and Suffix Pairs I] ( ./solutions/3042-count-prefix-and-suffix-pairs-i.js ) |Easy|
2148
2149
3066|[ Minimum Operations to Exceed Threshold Value II] ( ./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js ) |Medium|
2149
2150
3068|[ Find the Maximum Sum of Node Values] ( ./solutions/3068-find-the-maximum-sum-of-node-values.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 3039. Apply Operations to Make String Empty
3
+ * https://leetcode.com/problems/apply-operations-to-make-string-empty/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a string s.
7
+ *
8
+ * Consider performing the following operation until s becomes empty:
9
+ * - For every alphabet character from 'a' to 'z', remove the first occurrence of that
10
+ * character in s (if it exists).
11
+ *
12
+ * For example, let initially s = "aabcbbca". We do the following operations:
13
+ * - Remove the underlined characters s = "aabcbbca". The resulting string is s = "abbca".
14
+ * - Remove the underlined characters s = "abbca". The resulting string is s = "ba".
15
+ * - Remove the underlined characters s = "ba". The resulting string is s = "".
16
+ *
17
+ * Return the value of the string s right before applying the last operation. In the
18
+ * example above, answer is "ba".
19
+ */
20
+
21
+ /**
22
+ * @param {string } s
23
+ * @return {string }
24
+ */
25
+ var lastNonEmptyString = function ( s ) {
26
+ const charCount = new Array ( 26 ) . fill ( 0 ) ;
27
+ let maxFrequency = 0 ;
28
+
29
+ for ( const char of s ) {
30
+ const index = char . charCodeAt ( 0 ) - 97 ;
31
+ charCount [ index ] ++ ;
32
+ maxFrequency = Math . max ( maxFrequency , charCount [ index ] ) ;
33
+ }
34
+
35
+ let result = '' ;
36
+ for ( let i = s . length - 1 ; i >= 0 ; i -- ) {
37
+ const index = s . charCodeAt ( i ) - 97 ;
38
+ if ( charCount [ index ] === maxFrequency ) {
39
+ result = s [ i ] + result ;
40
+ charCount [ index ] -- ;
41
+ }
42
+ }
43
+
44
+ return result ;
45
+ } ;
You can’t perform that action at this time.
0 commit comments