Skip to content

Commit

Permalink
Add solution and test-cases for problem 3223
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jan 13, 2025
1 parent d569e0c commit 0ff19c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# [3223.Minimum Length of String After Operations][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
You are given a string `s`.

You can perform the following process on `s` **any** number of times:

- 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]`.
- Delete the **closest** character to the **left** of index `i` that is equal to `s[i]`.
- Delete the **closest** character to the **right** of index `i` that is equal to `s[i]`.

Return the **minimum** length of the final string `s` that you can achieve.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: s = "abaacbcbb"
## 题意
> ...
Output: 5
## 题解
Explanation:
We do the following operations:
### 思路1
> ...
Minimum Length of String After Operations
```go
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
```

**Example 2:**

```
Input: s = "aa"
Output: 2
Explanation:
We cannot perform any operations, so we return the length of the original string.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
count := [26]int{}
for _, b := range s {
count[b-'a']++
}
ans := 0
for i := range 26 {
if count[i] != 0 {
if count[i]&1 == 1 {
ans++
continue
}
ans += 2
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "abaacbcbb", 5},
{"TestCase2", "aa", 2},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 0ff19c6

Please sign in to comment.