Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi

<!-- solution:start -->

### 方法一
### 方法一:计数

根据题目描述,只要相邻字符不同,我们就可以移除它们。因此,最终剩下的字符串中只会包含相同字符,即全部是 'a' 或全部是 'b'。所以我们只需要计算字符串中 'a' 和 'b' 的数量,最终的最小长度就是两者数量的差的绝对值。

时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Mi

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
def minLengthAfterRemovals(self, s: str) -> int:
a = s.count("a")
b = len(s) - a
return abs(a - b)
Original file line number Diff line number Diff line change
@@ -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);
}