-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumStringLengthAfterRemovingSubstrings.ts
More file actions
43 lines (37 loc) · 1.46 KB
/
minimumStringLengthAfterRemovingSubstrings.ts
File metadata and controls
43 lines (37 loc) · 1.46 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// First Approach - Iterating over s length and removing certain substrings with substring string method. (102ms - Beats 19.36%)
// function minLength(s: string): number {
// let i = 0, j = 1;
// while(j < s.length) {
// const checkSubstring1 = s[i] === "A" && s[j] === "B";
// const checkSubstring2 = s[i] === "C" && s[j] === "D";
// if(checkSubstring1 || checkSubstring2) {
// s = s.substring(0, i) + s.substring(j + 1);
// i--; j--;
// continue;
// }
// i++; j++;
// }
// return s.length;
// };
// Second Approach - Stack approach iterating over s string and pushing back characters whenever it doesn't meet the 'AB' and 'CD' substring comparing with last added char, otherwise pop the stack, returning at the end the stack length. (https://leetcode.com/problems/minimum-string-length-after-removing-substrings/solutions/5879675/easy-stack-solution-python-java-c-o-n-o-n/?envType=daily-question&envId=2024-10-07)
function minLength(s: string): number {
const sStack: string[] = [];
for(let char of s) {
if(sStack.length === 0) {
sStack.push(char);
continue;
}
if(char === "B" && sStack[sStack.length - 1] === "A") {
sStack.pop();
} else if(char === "D" && sStack[sStack.length - 1] === "C") {
sStack.pop();
} else {
sStack.push(char);
}
}
return sStack.length;
};
const case1 = minLength("ABFCACDB");
const case2 = minLength("ACBBD");
console.log(`case1: ${case1} // ${case1 === 2}`);
console.log(`case2: ${case2} // ${case2 === 5}`);