Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c7788b

Browse files
committed
Add solution #3039
1 parent 63bb953 commit 9c7788b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,7 @@
21442144
3034|[Number of Subarrays That Match a Pattern I](./solutions/3034-number-of-subarrays-that-match-a-pattern-i.js)|Medium|
21452145
3035|[Maximum Palindromes After Operations](./solutions/3035-maximum-palindromes-after-operations.js)|Medium|
21462146
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|
21472148
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21482149
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21492150
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
};

0 commit comments

Comments
 (0)