diff --git a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/README.md b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/README.md index 14fd0d8fe..5bfb0c89c 100755 --- a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/README.md +++ b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution.go b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution.go index d115ccf5e..b8be5898e 100644 --- a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution.go +++ b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution.go @@ -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 } diff --git a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution_test.go b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution_test.go index 14ff50eb4..045dcca7f 100644 --- a/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution_test.go +++ b/leetcode/3201-3300/3223.Minimum-Length-of-String-After-Operations/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }