From 309a77f18e65080f2fa59d401944236c5a992cb6 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 4 Jul 2025 09:00:34 +0800 Subject: [PATCH] Add solution and test-cases for problem 3307 --- .../README.md | 49 ++++++++++++++----- .../Solution.go | 17 ++++++- .../Solution_test.go | 22 ++++----- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/README.md b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/README.md index 338708360..fc26da463 100755 --- a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/README.md +++ b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/README.md @@ -1,28 +1,51 @@ # [3307.Find the K-th Character in String Game II][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. You are also given an integer array `operations`, where `operations[i]` represents the **type** of the `ith` operation. + +Now Bob will ask Alice to perform **all** operations in sequence: + +- If `operations[i] == 0`, **append** a copy of `word` to itself. +- If `operations[i] == 1`, 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 performing all the operations. + +**Note** that the character `'z'` can be changed to `'a'` in the second type of operation. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: k = 5, operations = [0,0,0] + +Output: "a" -## 题意 -> ... +Explanation: -## 题解 +Initially, word == "a". Alice performs the three operations as follows: -### 思路1 -> ... -Find the K-th Character in String Game II -```go +Appends "a" to "a", word becomes "aa". +Appends "aa" to "aa", word becomes "aaaa". +Appends "aaaa" to "aaaa", word becomes "aaaaaaaa". ``` +**Example 2:** + +``` +Input: k = 10, operations = [0,1,0,1] + +Output: "b" + +Explanation: + +Initially, word == "a". Alice performs the four operations as follows: + +Appends "a" to "a", word becomes "aa". +Appends "bb" to "aa", word becomes "aabb". +Appends "aabb" to "aabb", word becomes "aabbaabb". +Appends "bbccbbcc" to "aabbaabb", word becomes "aabbaabbbbccbbcc". +``` ## 结语 diff --git a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution.go b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution.go index d115ccf5e..a81c445fd 100644 --- a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution.go +++ b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +import "math/bits" + +func Solution(k int64, operations []int) byte { + ans := 0 + for k != 1 { + t := bits.Len64(uint64(k)) - 1 + if (1 << t) == k { + t-- + } + k -= (1 << t) + if operations[t] != 0 { + ans++ + } + } + return byte('a' + (ans % 26)) } diff --git a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution_test.go b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution_test.go index 14ff50eb4..b82bc1986 100644 --- a/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution_test.go +++ b/leetcode/3301-3400/3307.Find-the-K-th-Character-in-String-Game-II/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + k int64 + operations []int + expect byte }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, []int{0, 0, 0}, 'a'}, + {"TestCase2", 10, []int{0, 1, 0, 1}, 'b'}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.k, c.operations) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.k, c.operations) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }