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 e399609fa2ebd..ee4d646a0ed9a 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,44 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 { + let (mut a, mut b) = ('a', 'b'); + if x < y { + std::mem::swap(&mut x, &mut y); + std::mem::swap(&mut a, &mut b); + } + + let mut ans = 0; + let mut cnt1 = 0; + let mut cnt2 = 0; + + for c in s.chars() { + if c == a { + cnt1 += 1; + } else if c == b { + if cnt1 > 0 { + ans += x; + cnt1 -= 1; + } else { + cnt2 += 1; + } + } else { + ans += cnt1.min(cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + + ans += cnt1.min(cnt2) * y; + ans + } +} +``` + #### JavaScript ```js @@ -291,81 +329,38 @@ function maximumGain(s, x, y) { } ``` - - - - - - -### 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; +#### C# - const multiplier = p === 'a' ? x : y; - ans += (multiplier * (str.length - stk.length)) / 2; - str = [...stk]; - stk.length = 0; +```cs +public class Solution { + public int MaximumGain(string s, int x, int y) { + char a = 'a', b = 'b'; + if (x < y) { + (x, y) = (y, x); + (a, b) = (b, a); } - } - - 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); + int ans = 0, cnt1 = 0, cnt2 = 0; + foreach (char c in s) { + if (c == a) { + cnt1++; + } else if (c == b) { + if (cnt1 > 0) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.Min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; } - - 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; + ans += Math.Min(cnt1, cnt2) * y; + 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 cd7204fd75dcf..5bd2436a0f9ed 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,44 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 { + let (mut a, mut b) = ('a', 'b'); + if x < y { + std::mem::swap(&mut x, &mut y); + std::mem::swap(&mut a, &mut b); + } + + let mut ans = 0; + let mut cnt1 = 0; + let mut cnt2 = 0; + + for c in s.chars() { + if c == a { + cnt1 += 1; + } else if c == b { + if cnt1 > 0 { + ans += x; + cnt1 -= 1; + } else { + cnt2 += 1; + } + } else { + ans += cnt1.min(cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + + ans += cnt1.min(cnt2) * y; + ans + } +} +``` + #### JavaScript ```js @@ -291,81 +329,38 @@ function maximumGain(s, x, y) { } ``` - - - - - - -### 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; +#### C# - const multiplier = p === 'a' ? x : y; - ans += (multiplier * (str.length - stk.length)) / 2; - str = [...stk]; - stk.length = 0; +```cs +public class Solution { + public int MaximumGain(string s, int x, int y) { + char a = 'a', b = 'b'; + if (x < y) { + (x, y) = (y, x); + (a, b) = (b, a); } - } - - 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); + int ans = 0, cnt1 = 0, cnt2 = 0; + foreach (char c in s) { + if (c == a) { + cnt1++; + } else if (c == b) { + if (cnt1 > 0) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.Min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; } - - 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; + ans += Math.Min(cnt1, cnt2) * y; + return ans; + } } ``` diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cs b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cs new file mode 100644 index 0000000000000..14b0ed2e9eed1 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cs @@ -0,0 +1,30 @@ +public class Solution { + public int MaximumGain(string s, int x, int y) { + char a = 'a', b = 'b'; + if (x < y) { + (x, y) = (y, x); + (a, b) = (b, a); + } + + int ans = 0, cnt1 = 0, cnt2 = 0; + foreach (char c in s) { + if (c == a) { + cnt1++; + } else if (c == b) { + if (cnt1 > 0) { + 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/Solution.rs b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.rs new file mode 100644 index 0000000000000..b36a061bd38bc --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.rs @@ -0,0 +1,33 @@ +impl Solution { + pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 { + let (mut a, mut b) = ('a', 'b'); + if x < y { + std::mem::swap(&mut x, &mut y); + std::mem::swap(&mut a, &mut b); + } + + let mut ans = 0; + let mut cnt1 = 0; + let mut cnt2 = 0; + + for c in s.chars() { + if c == a { + cnt1 += 1; + } else if c == b { + if cnt1 > 0 { + ans += x; + cnt1 -= 1; + } else { + cnt2 += 1; + } + } else { + ans += cnt1.min(cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + + ans += cnt1.min(cnt2) * y; + 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 deleted file mode 100644 index 52b060f133f78..0000000000000 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index f4ed700d88a27..0000000000000 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts +++ /dev/null @@ -1,29 +0,0 @@ -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; -}