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

Commit 78a451f

Browse files
committed
Add solution #2696
1 parent ae23c0b commit 78a451f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,7 @@
19911991
2693|[Call Function with Custom Context](./solutions/2693-call-function-with-custom-context.js)|Medium|
19921992
2694|[Event Emitter](./solutions/2694-event-emitter.js)|Medium|
19931993
2695|[Array Wrapper](./solutions/2695-array-wrapper.js)|Easy|
1994+
2696|[Minimum String Length After Removing Substrings](./solutions/2696-minimum-string-length-after-removing-substrings.js)|Easy|
19941995
2698|[Find the Punishment Number of an Integer](./solutions/2698-find-the-punishment-number-of-an-integer.js)|Medium|
19951996
2703|[Return Length of Arguments Passed](./solutions/2703-return-length-of-arguments-passed.js)|Easy|
19961997
2704|[To Be Or Not To Be](./solutions/2704-to-be-or-not-to-be.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2696. Minimum String Length After Removing Substrings
3+
* https://leetcode.com/problems/minimum-string-length-after-removing-substrings/
4+
* Difficulty: Easy
5+
*
6+
* You are given a string s consisting only of uppercase English letters.
7+
*
8+
* You can apply some operations to this string where, in one operation, you can remove any
9+
* occurrence of one of the substrings "AB" or "CD" from s.
10+
*
11+
* Return the minimum possible length of the resulting string that you can obtain.
12+
*
13+
* Note that the string concatenates after removing the substring and could produce new "AB"
14+
* or "CD" substrings.
15+
*/
16+
17+
/**
18+
* @param {string} s
19+
* @return {number}
20+
*/
21+
var minLength = function(s) {
22+
const stack = [];
23+
24+
for (const char of s) {
25+
if (stack.length && ((stack[stack.length - 1] === 'A' && char === 'B')
26+
|| (stack[stack.length - 1] === 'C' && char === 'D'))) {
27+
stack.pop();
28+
} else {
29+
stack.push(char);
30+
}
31+
}
32+
33+
return stack.length;
34+
};

0 commit comments

Comments
 (0)