From d1634f858c6eca3accd48e6582afc653a3fc36c0 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 7 Jun 2025 20:32:18 +0800 Subject: [PATCH] Add solution and test-cases for problem 3170 --- .../README.md | 35 ++++++++++++------- .../Solution.go | 29 +++++++++++++-- .../Solution_test.go | 13 ++++--- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/README.md b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/README.md index 61fb19595..5b4b95a03 100755 --- a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/README.md +++ b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/README.md @@ -1,28 +1,37 @@ # [3170.Lexicographically Minimum String After Removing Stars][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`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters. + +While there is a `'*'`, do the following operation: + +- Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its left. If there are several smallest characters, you can delete any of them. + +Return the **lexicographically smallest** resulting string after removing all `'*'` characters. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: s = "aaba*" -## 题意 -> ... +Output: "aab" -## 题解 +Explanation: -### 思路1 -> ... -Lexicographically Minimum String After Removing Stars -```go +We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest. ``` +**Example 2:** + +``` +Input: s = "abc" + +Output: "abc" + +Explanation: + +There is no '*' in the string. +``` ## 结语 diff --git a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution.go b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution.go index d115ccf5e..0e12b604b 100644 --- a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution.go +++ b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution.go @@ -1,5 +1,30 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) string { + cnt := make([][]int, 26) + for i := range cnt { + cnt[i] = make([]int, 0) + } + arr := []rune(s) + for i, c := range arr { + if c != '*' { + cnt[c-'a'] = append(cnt[c-'a'], i) + } else { + for j := 0; j < 26; j++ { + if len(cnt[j]) > 0 { + last := len(cnt[j]) - 1 + arr[cnt[j][last]] = '*' + cnt[j] = cnt[j][:last] + break + } + } + } + } + var ans []rune + for _, c := range arr { + if c != '*' { + ans = append(ans, c) + } + } + return string(ans) } diff --git a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution_test.go b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution_test.go index 14ff50eb4..40855c170 100644 --- a/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/Solution_test.go +++ b/leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/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", "aaba*", "aab"}, + {"TestCase2", "abc", "abc"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }