-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-score-from-removing-substrings.js
39 lines (35 loc) · 1.66 KB
/
maximum-score-from-removing-substrings.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
28
29
30
31
32
33
34
35
36
37
38
39
//maximum score from removing substring
//12th-july-2024
//String, stack, greedy
var maximumGain = function (s, x, y) {
// determine the order of 'x' and 'y' based on their potential score contribution
const [first, second] = x > y ? ['ab', 'ba'] : ['ba', 'ab']; // destructuring assignment
// destructure again to store the higher and lower scoring sequences
const [highScore, lowScore] = x > y ? [x, y] : [y, x];
let score = 0;
let stack = [];
// iterate through each character in the string 's'
for (let char of s) {
// check if the stack is not empty and the last character combined with the current
// character forms the 'first' sequence (higher scoring)
if (stack.length && stack[stack.length - 1] + char === first) {
stack.pop(); // remove the last character from the stack
score += highScore; // update score with the higher scoring sequence
} else {
stack.push(char); // if not a match, push the current character onto the stack
}
}
let newStack = []; // create a new stack to handle the remaining characters
// iterate through the characters remaining in the stack after processing 's'
for (let char of stack) {
// check if the new stack is not empty and the last character combined with the current
// character forms the 'second' sequence (lower scoring)
if (newStack.length && newStack[newStack.length - 1] + char === second) {
newStack.pop(); // remove the last character from the new stack
score += lowScore; // update score with the lower scoring sequence
} else {
newStack.push(char); // if not a match, push the character onto the new stack
}
}
return score;
};