Skip to content

Commit 544046f

Browse files
authored
Merge pull request #1093 from 0xff-dev/3223
Add solution and test-cases for problem 3223
2 parents af27248 + 0ff19c6 commit 544046f

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [3223.Minimum Length of String After Operations][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a string `s`.
5+
6+
You can perform the following process on `s` **any** number of times:
7+
8+
- Choose an index `i` in the string such that there is **at least** one character to the left of index `i` that is equal to `s[i]`, and **at least** one character to the right that is also equal to `s[i]`.
9+
- Delete the **closest** character to the **left** of index `i` that is equal to `s[i]`.
10+
- Delete the **closest** character to the **right** of index `i` that is equal to `s[i]`.
11+
12+
Return the **minimum** length of the final string `s` that you can achieve.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
17+
Input: s = "abaacbcbb"
1418
15-
## 题意
16-
> ...
19+
Output: 5
1720
18-
## 题解
21+
Explanation:
22+
We do the following operations:
1923
20-
### 思路1
21-
> ...
22-
Minimum Length of String After Operations
23-
```go
24+
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
25+
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
2426
```
2527

28+
**Example 2:**
29+
30+
```
31+
Input: s = "aa"
32+
33+
Output: 2
34+
35+
Explanation:
36+
We cannot perform any operations, so we return the length of the original string.
37+
```
2638

2739
## 结语
2840

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
count := [26]int{}
5+
for _, b := range s {
6+
count[b-'a']++
7+
}
8+
ans := 0
9+
for i := range 26 {
10+
if count[i] != 0 {
11+
if count[i]&1 == 1 {
12+
ans++
13+
continue
14+
}
15+
ans += 2
16+
}
17+
}
18+
return ans
519
}

leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abaacbcbb", 5},
17+
{"TestCase2", "aa", 2},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)