From 03a916e9b847f28231801edfb1b39fb3ec0e961d Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 6 Jun 2025 09:29:29 +0800 Subject: [PATCH] Add solution and test-cases for problem 2434 --- .../README.md | 44 +++++++++++++------ .../Solution.go | 24 +++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/README.md b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/README.md index 80b5e18cc..f7e2ed46b 100755 --- a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/README.md +++ b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/README.md @@ -1,28 +1,46 @@ # [2434.Using a Robot to Print the Lexicographically Smallest String][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` and a robot that currently holds an empty string `t`. Apply one of the following operations until `t` and `t` **are both empty**: + +- Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`. +- Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper. + +Return the lexicographically smallest string that can be written on the paper. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "zza" +Output: "azz" +Explanation: Let p denote the written string. +Initially p="", s="zza", t="". +Perform first operation three times p="", s="", t="zza". +Perform second operation three times p="azz", s="", t="". ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Using a Robot to Print the Lexicographically Smallest String -```go ``` +Input: s = "bac" +Output: "abc" +Explanation: Let p denote the written string. +Perform first operation twice p="", s="c", t="ba". +Perform second operation twice p="ab", s="c", t="". +Perform first operation p="ab", s="", t="c". +Perform second operation p="abc", s="", t="". +``` + +**Example 3:** +``` +Input: s = "bdda" +Output: "addb" +Explanation: Let p denote the written string. +Initially p="", s="bdda", t="". +Perform first operation four times p="", s="", t="bdda". +Perform second operation four times p="addb", s="", t="". +``` ## 结语 diff --git a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution.go b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution.go index d115ccf5e..dafb42a81 100644 --- a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution.go +++ b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) string { + cnt := make([]int, 26) + for _, c := range s { + cnt[c-'a']++ + } + + stack := []rune{} + res := []rune{} + minCharacter := 'a' + for _, c := range s { + stack = append(stack, c) + cnt[c-'a']-- + for minCharacter != 'z' && cnt[minCharacter-'a'] == 0 { + minCharacter++ + } + for len(stack) > 0 && stack[len(stack)-1] <= minCharacter { + res = append(res, stack[len(stack)-1]) + stack = stack[:len(stack)-1] + } + } + + return string(res) } diff --git a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution_test.go b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution_test.go index 14ff50eb4..0700f8559 100644 --- a/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution_test.go +++ b/leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution_test.go @@ -10,12 +10,12 @@ 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", "zza", "azz"}, + {"TestCase2", "bac", "abc"}, + {"TestCase3", "bdda", "addb"}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }