-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2696MinimumStringLengthAfterRemovingSubstrings.js
27 lines (27 loc) · 1.32 KB
/
2696MinimumStringLengthAfterRemovingSubstrings.js
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
// 2696. Minimum String Length After Removing Substrings
function minLength(s) {
// You are given a string s consisting only of uppercase English letters.
// You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s.
// Return the minimum possible length of the resulting string that you can obtain.
// Note that the string concatenates after removing the substring and could produce new "AB" or "CD" substrings.
//
// Because they never overlap, theres no point in choosing to not remove it.
// So you must always remove it when you find it.
// So we can just iterate through backwards, removing them as wel go, and return remaining len
// let currentString = s
// while(currentString != currentString.replaceAll(/(AB|CD)/g,"")){
// currentString = currentString.replaceAll(/(AB|CD)/g,"")
// }
// return currentString.length
let currentString = s;
while (currentString.indexOf("AB") !== -1 || currentString.indexOf("CD") !== -1) {
for (let i = 0; i < currentString.length - 1; i++) {
const char = currentString[i] + currentString[i + 1];
if (["AB", "CD"].includes(char)) {
currentString = currentString.slice(0, i) + currentString.slice(i + 2);
i -= 2;
}
}
}
return currentString.length;
}