From 5d19c0050f2171f8d90f2bfc1bd11639dad268d2 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 17 Jun 2024 08:58:02 +0800 Subject: [PATCH] Add solution and test-cases for problem 3039 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 27 +++++++++++++- .../Solution_test.go | 13 +++---- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/README.md b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/README.md index 96bb467a0..c84dfe227 100755 --- a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/README.md +++ b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/README.md @@ -1,28 +1,37 @@ # [3039.Apply Operations to Make String Empty][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`. + +Consider performing the following operation until `s` becomes **empty**: + +- For **every** alphabet character from `'a'` to `'z'`, remove the **first** occurrence of that character in `s` (if it exists). + +For example, let initially `s = "aabcbbca"`. We do the following operations: + +- Remove the underlined characters `s = "aabcbbca"`. The resulting string is `s = "abbca"`. +- Remove the underlined characters `s = "abbca"`. The resulting string is `s = "ba"`. +- Remove the underlined characters `s = "ba"`. The resulting string is `s = ""`. + +Return the value of the string `s` right **before** applying the **last** operation. In the example above, answer is `"ba"`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "aabcbbca" +Output: "ba" +Explanation: Explained in the statement. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Apply Operations to Make String Empty -```go ``` - +Input: s = "abcd" +Output: "abcd" +Explanation: We do the following operation: +- Remove the underlined characters s = "abcd". The resulting string is s = "". +The string just before the last operation is "abcd". +``` ## 结语 diff --git a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution.go b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution.go index d115ccf5e..ebcc3634f 100644 --- a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution.go +++ b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "sort" + "strings" +) + +func Solution(s string) string { + count := [26][2]int{} + mc := 0 + for i, b := range s { + count[b-'a'][0]++ + count[b-'a'][1] = i + mc = max(mc, count[b-'a'][0]) + } + indies := []int{} + for i := 0; i < 26; i++ { + if count[i][0] == mc { + indies = append(indies, count[i][1]) + } + } + sort.Ints(indies) + sb := strings.Builder{} + for _, i := range indies { + sb.WriteByte(s[i]) + } + return sb.String() } diff --git a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution_test.go b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution_test.go index 14ff50eb4..f4dc5b4b8 100644 --- a/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution_test.go +++ b/leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "aabcbbca", "ba"}, + {"TestCase2", "abcd", "abcd"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }