diff --git a/leetcode/3201-3300/3271.Hash-Divided-String/README.md b/leetcode/3201-3300/3271.Hash-Divided-String/README.md index 123613c87..cb6446a1d 100755 --- a/leetcode/3201-3300/3271.Hash-Divided-String/README.md +++ b/leetcode/3201-3300/3271.Hash-Divided-String/README.md @@ -1,28 +1,45 @@ # [3271.Hash Divided 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` of length `n` and an integer `k`, where `n` is a **multiple** of `k`. Your task is to hash the string `s` into a new string called `result`, which has a length of `n / k`. + +First, divide `s` into `n / k` substrings, each with a length of `k`. Then, initialize `result` as an **empty** string. + +For each **substring** in order from the beginning: + +- The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0, 'b' → 1, ..., 'z' → 25`). +- Calculate the sum of all the **hash values** of the characters in the substring. +- Find the remainder of this sum when divided by 26, which is called `hashedChar`. +- Identify the character in the English lowercase alphabet that corresponds to `hashedChar`. +- Append that character to the end of `result`. + +Return `result`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: s = "abcd", k = 2 + +Output: "bf" -## 题意 -> ... +Explanation: -## 题解 +First substring: "ab", 0 + 1 = 1, 1 % 26 = 1, result[0] = 'b'. -### 思路1 -> ... -Hash Divided String -```go +Second substring: "cd", 2 + 3 = 5, 5 % 26 = 5, result[1] = 'f'. ``` +**Example 2:** + +``` +Input: s = "mxz", k = 3 + +Output: "i" + +Explanation: + +The only substring: "mxz", 12 + 23 + 25 = 60, 60 % 26 = 8, result[0] = 'i'. +``` ## 结语 diff --git a/leetcode/3201-3300/3271.Hash-Divided-String/Solution.go b/leetcode/3201-3300/3271.Hash-Divided-String/Solution.go index d115ccf5e..0b4625119 100644 --- a/leetcode/3201-3300/3271.Hash-Divided-String/Solution.go +++ b/leetcode/3201-3300/3271.Hash-Divided-String/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +import "bytes" + +func Solution(s string, k int) string { + buf := bytes.NewBuffer([]byte{}) + sum := 0 + for i := 0; i < len(s); i++ { + if i != 0 && i%k == 0 { + buf.WriteByte(byte(sum%26 + 'a')) + sum = int(s[i]-'a') + continue + } + sum += int(s[i] - 'a') + } + buf.WriteByte(byte(sum%26 + 'a')) + return buf.String() } diff --git a/leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go b/leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go index 14ff50eb4..d6843d174 100644 --- a/leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go +++ b/leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + k int + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abcd", 2, "bf"}, + {"TestCase2", "mxz", 3, "i"}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.k) 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.inputs, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }