diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README.md b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README.md index c9a589a7e66b4..6fac4879ed63b 100644 --- a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README.md +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README.md @@ -76,32 +76,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi -### 方法一 +### 方法一:计数 + +根据题目描述,只要相邻字符不同,我们就可以移除它们。因此,最终剩下的字符串中只会包含相同字符,即全部是 'a' 或全部是 'b'。所以我们只需要计算字符串中 'a' 和 'b' 的数量,最终的最小长度就是两者数量的差的绝对值。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minLengthAfterRemovals(self, s: str) -> int: + a = s.count("a") + b = len(s) - a + return abs(a - b) ``` #### Java ```java - +class Solution { + public int minLengthAfterRemovals(String s) { + int n = s.length(); + int a = 0; + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == 'a') { + ++a; + } + } + int b = n - a; + return Math.abs(a - b); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minLengthAfterRemovals(string s) { + int a = 0; + for (char c : s) { + if (c == 'a') { + ++a; + } + } + int b = s.size() - a; + return abs(a - b); + } +}; ``` #### Go ```go +func minLengthAfterRemovals(s string) int { + a := strings.Count(s, "a") + b := len(s) - a + return abs(a - b) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function minLengthAfterRemovals(s: string): number { + let a = 0; + for (const c of s) { + if (c === 'a') { + ++a; + } + } + const b = s.length - a; + return Math.abs(a - b); +} ``` diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README_EN.md b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README_EN.md index e2f765eaafbb5..99d20d0b352f2 100644 --- a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README_EN.md +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/README_EN.md @@ -71,32 +71,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi -### Solution 1 +### Solution 1: Counting + +According to the problem description, as long as adjacent characters are different, we can remove them. Therefore, the final remaining string will only contain the same character, either all 'a' or all 'b'. So we only need to count the number of 'a' and 'b' in the string, and the final minimum length is the absolute difference between their counts. + +The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def minLengthAfterRemovals(self, s: str) -> int: + a = s.count("a") + b = len(s) - a + return abs(a - b) ``` #### Java ```java - +class Solution { + public int minLengthAfterRemovals(String s) { + int n = s.length(); + int a = 0; + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == 'a') { + ++a; + } + } + int b = n - a; + return Math.abs(a - b); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minLengthAfterRemovals(string s) { + int a = 0; + for (char c : s) { + if (c == 'a') { + ++a; + } + } + int b = s.size() - a; + return abs(a - b); + } +}; ``` #### Go ```go +func minLengthAfterRemovals(s string) int { + a := strings.Count(s, "a") + b := len(s) - a + return abs(a - b) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function minLengthAfterRemovals(s: string): number { + let a = 0; + for (const c of s) { + if (c === 'a') { + ++a; + } + } + const b = s.length - a; + return Math.abs(a - b); +} ``` diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.cpp b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.cpp new file mode 100644 index 0000000000000..f3227c197e429 --- /dev/null +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int minLengthAfterRemovals(string s) { + int a = 0; + for (char c : s) { + if (c == 'a') { + ++a; + } + } + int b = s.size() - a; + return abs(a - b); + } +}; diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.go b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.go new file mode 100644 index 0000000000000..5a31a1e6f03f9 --- /dev/null +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.go @@ -0,0 +1,12 @@ +func minLengthAfterRemovals(s string) int { + a := strings.Count(s, "a") + b := len(s) - a + return abs(a - b) +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.java b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.java new file mode 100644 index 0000000000000..2300360046969 --- /dev/null +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int minLengthAfterRemovals(String s) { + int n = s.length(); + int a = 0; + for (int i = 0; i < n; ++i) { + if (s.charAt(i) == 'a') { + ++a; + } + } + int b = n - a; + return Math.abs(a - b); + } +} diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.py b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.py new file mode 100644 index 0000000000000..608b487189d02 --- /dev/null +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.py @@ -0,0 +1,5 @@ +class Solution: + def minLengthAfterRemovals(self, s: str) -> int: + a = s.count("a") + b = len(s) - a + return abs(a - b) diff --git a/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.ts b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.ts new file mode 100644 index 0000000000000..5f0464ee51140 --- /dev/null +++ b/solution/3700-3799/3746.Minimum String Length After Balanced Removals/Solution.ts @@ -0,0 +1,10 @@ +function minLengthAfterRemovals(s: string): number { + let a = 0; + for (const c of s) { + if (c === 'a') { + ++a; + } + } + const b = s.length - a; + return Math.abs(a - b); +}