From 7d9cae57ccb5491645550c608cb85374eec5a5b1 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 3 Jul 2025 09:01:01 +0800 Subject: [PATCH] Add solution and test-cases for problem 3304 --- .../README.md | 41 +++++++++++++------ .../Solution.go | 12 +++++- .../Solution_test.go | 13 +++--- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/README.md b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/README.md index be2a86e6e..8f5c4ac7c 100755 --- a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/README.md +++ b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/README.md @@ -1,28 +1,43 @@ # [3304.Find the K-th Character in String Game I][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 +Alice and Bob are playing a game. Initially, Alice has a string `word = "a"`. + +You are given a **positive** integer `k`. + +Now Bob will ask Alice to perform the following operation **forever**: + +- Generate a new string by **changing** each character in `word` to its **next** character in the English alphabet, and **append** it to the original `word`. + +For example, performing the operation on `"c"` generates `"cd"` and performing the operation on `"zb"` generates `"zbac"`. + +Return the value of the `kth` character in `word`, after enough operations have been done for `word` to have **at least** `k` characters. + +**Note** that the character `'z'` can be changed to `'a'` in the operation. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: k = 5 + +Output: "b" + +Explanation: -## 题意 -> ... +Initially, word = "a". We need to do the operation three times: -## 题解 +Generated string is "b", word becomes "ab". +Generated string is "bc", word becomes "abbc". +Generated string is "bccd", word becomes "abbcbccd". +``` + +**Example 2:** -### 思路1 -> ... -Find the K-th Character in String Game I -```go ``` +Input: k = 10 +Output: "c" +``` ## 结语 diff --git a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution.go b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution.go index d115ccf5e..d5aff5e5b 100644 --- a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution.go +++ b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution.go @@ -1,5 +1,13 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(k int) byte { + str := "a" + for len(str) < k { + tmp := []byte(str) + for i := range tmp { + tmp[i] = (tmp[i]-'a'+1)%26 + 'a' + } + str += string(tmp) + } + return str[k-1] } diff --git a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution_test.go b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution_test.go index 14ff50eb4..2ef09f12a 100644 --- a/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution_test.go +++ b/leetcode/3301-3400/3304.Find-the-K-th-Character-in-String-Game-I/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs int + expect byte }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, 'b'}, + {"TestCase2", 10, 'c'}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }