Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions leetcode/3201-3300/3271.Hash-Divided-String/README.md
Original file line number Diff line number Diff line change
@@ -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'.
```

## 结语

Expand Down
17 changes: 15 additions & 2 deletions leetcode/3201-3300/3271.Hash-Divided-String/Solution.go
Original file line number Diff line number Diff line change
@@ -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()
}
20 changes: 10 additions & 10 deletions leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading