Skip to content

Commit c849580

Browse files
committed
feat: add solutions to lc problem: No.3746
1 parent fe167c6 commit c849580

File tree

7 files changed

+177
-8
lines changed

7 files changed

+177
-8
lines changed

solution/3700-3799/3746.Minimum String Length After Balanced Removals/README.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi
7676

7777
<!-- solution:start -->
7878

79-
### 方法一
79+
### 方法一:计数
80+
81+
根据题目描述,只要相邻字符不同,我们就可以移除它们。因此,最终剩下的字符串中只会包含相同字符,即全部是 'a' 或全部是 'b'。所以我们只需要计算字符串中 'a' 和 'b' 的数量,最终的最小长度就是两者数量的差的绝对值。
82+
83+
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。
8084

8185
<!-- tabs:start -->
8286

8387
#### Python3
8488

8589
```python
86-
90+
class Solution:
91+
def minLengthAfterRemovals(self, s: str) -> int:
92+
a = s.count("a")
93+
b = len(s) - a
94+
return abs(a - b)
8795
```
8896

8997
#### Java
9098

9199
```java
92-
100+
class Solution {
101+
public int minLengthAfterRemovals(String s) {
102+
int n = s.length();
103+
int a = 0;
104+
for (int i = 0; i < n; ++i) {
105+
if (s.charAt(i) == 'a') {
106+
++a;
107+
}
108+
}
109+
int b = n - a;
110+
return Math.abs(a - b);
111+
}
112+
}
93113
```
94114

95115
#### C++
96116

97117
```cpp
98-
118+
class Solution {
119+
public:
120+
int minLengthAfterRemovals(string s) {
121+
int a = 0;
122+
for (char c : s) {
123+
if (c == 'a') {
124+
++a;
125+
}
126+
}
127+
int b = s.size() - a;
128+
return abs(a - b);
129+
}
130+
};
99131
```
100132
101133
#### Go
102134
103135
```go
136+
func minLengthAfterRemovals(s string) int {
137+
a := strings.Count(s, "a")
138+
b := len(s) - a
139+
return abs(a - b)
140+
}
141+
142+
func abs(x int) int {
143+
if x < 0 {
144+
return -x
145+
}
146+
return x
147+
}
148+
```
104149

150+
#### TypeScript
151+
152+
```ts
153+
function minLengthAfterRemovals(s: string): number {
154+
let a = 0;
155+
for (const c of s) {
156+
if (c === 'a') {
157+
++a;
158+
}
159+
}
160+
const b = s.length - a;
161+
return Math.abs(a - b);
162+
}
105163
```
106164

107165
<!-- tabs:end -->

solution/3700-3799/3746.Minimum String Length After Balanced Removals/README_EN.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Counting
75+
76+
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.
77+
78+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
7579

7680
<!-- tabs:start -->
7781

7882
#### Python3
7983

8084
```python
81-
85+
class Solution:
86+
def minLengthAfterRemovals(self, s: str) -> int:
87+
a = s.count("a")
88+
b = len(s) - a
89+
return abs(a - b)
8290
```
8391

8492
#### Java
8593

8694
```java
87-
95+
class Solution {
96+
public int minLengthAfterRemovals(String s) {
97+
int n = s.length();
98+
int a = 0;
99+
for (int i = 0; i < n; ++i) {
100+
if (s.charAt(i) == 'a') {
101+
++a;
102+
}
103+
}
104+
int b = n - a;
105+
return Math.abs(a - b);
106+
}
107+
}
88108
```
89109

90110
#### C++
91111

92112
```cpp
93-
113+
class Solution {
114+
public:
115+
int minLengthAfterRemovals(string s) {
116+
int a = 0;
117+
for (char c : s) {
118+
if (c == 'a') {
119+
++a;
120+
}
121+
}
122+
int b = s.size() - a;
123+
return abs(a - b);
124+
}
125+
};
94126
```
95127
96128
#### Go
97129
98130
```go
131+
func minLengthAfterRemovals(s string) int {
132+
a := strings.Count(s, "a")
133+
b := len(s) - a
134+
return abs(a - b)
135+
}
136+
137+
func abs(x int) int {
138+
if x < 0 {
139+
return -x
140+
}
141+
return x
142+
}
143+
```
99144

145+
#### TypeScript
146+
147+
```ts
148+
function minLengthAfterRemovals(s: string): number {
149+
let a = 0;
150+
for (const c of s) {
151+
if (c === 'a') {
152+
++a;
153+
}
154+
}
155+
const b = s.length - a;
156+
return Math.abs(a - b);
157+
}
100158
```
101159

102160
<!-- tabs:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minLengthAfterRemovals(string s) {
4+
int a = 0;
5+
for (char c : s) {
6+
if (c == 'a') {
7+
++a;
8+
}
9+
}
10+
int b = s.size() - a;
11+
return abs(a - b);
12+
}
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func minLengthAfterRemovals(s string) int {
2+
a := strings.Count(s, "a")
3+
b := len(s) - a
4+
return abs(a - b)
5+
}
6+
7+
func abs(x int) int {
8+
if x < 0 {
9+
return -x
10+
}
11+
return x
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minLengthAfterRemovals(String s) {
3+
int n = s.length();
4+
int a = 0;
5+
for (int i = 0; i < n; ++i) {
6+
if (s.charAt(i) == 'a') {
7+
++a;
8+
}
9+
}
10+
int b = n - a;
11+
return Math.abs(a - b);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def minLengthAfterRemovals(self, s: str) -> int:
3+
a = s.count("a")
4+
b = len(s) - a
5+
return abs(a - b)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minLengthAfterRemovals(s: string): number {
2+
let a = 0;
3+
for (const c of s) {
4+
if (c === 'a') {
5+
++a;
6+
}
7+
}
8+
const b = s.length - a;
9+
return Math.abs(a - b);
10+
}

0 commit comments

Comments
 (0)