diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md index 523febeecd7df..e399609fa2ebd 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md @@ -259,6 +259,116 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaScript + +```js +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} +``` + + + + + + + +### Solution 2: Greedy + Stack + + + +#### TypeScript + +```ts +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + +#### JavaeScript + +```js +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md index 3bd0568e9e45d..cd7204fd75dcf 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md @@ -259,6 +259,116 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaScript + +```js +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} +``` + + + + + + + +### Solution 2: Greedy + Stack + + + +#### TypeScript + +```ts +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + +#### JavaeScript + +```js +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js new file mode 100644 index 0000000000000..9e649683d53e1 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js @@ -0,0 +1,27 @@ +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js new file mode 100644 index 0000000000000..52b060f133f78 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js @@ -0,0 +1,29 @@ +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts new file mode 100644 index 0000000000000..f4ed700d88a27 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts @@ -0,0 +1,29 @@ +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +}