Skip to content

Commit 2866963

Browse files
authored
Merge pull request #1366 from 0xff-dev/3271
Add solution and test-cases for problem 3271
2 parents 5cbdb11 + 1eaa7e4 commit 2866963

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

leetcode/3201-3300/3271.Hash-Divided-String/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3271.Hash Divided String][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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`.
5+
6+
First, divide `s` into `n / k` substrings, each with a length of `k`. Then, initialize `result` as an **empty** string.
7+
8+
For each **substring** in order from the beginning:
9+
10+
- The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0, 'b' → 1, ..., 'z' → 25`).
11+
- Calculate the sum of all the **hash values** of the characters in the substring.
12+
- Find the remainder of this sum when divided by 26, which is called `hashedChar`.
13+
- Identify the character in the English lowercase alphabet that corresponds to `hashedChar`.
14+
- Append that character to the end of `result`.
15+
16+
Return `result`.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
21+
Input: s = "abcd", k = 2
22+
23+
Output: "bf"
1424
15-
## 题意
16-
> ...
25+
Explanation:
1726
18-
## 题解
27+
First substring: "ab", 0 + 1 = 1, 1 % 26 = 1, result[0] = 'b'.
1928
20-
### 思路1
21-
> ...
22-
Hash Divided String
23-
```go
29+
Second substring: "cd", 2 + 3 = 5, 5 % 26 = 5, result[1] = 'f'.
2430
```
2531

32+
**Example 2:**
33+
34+
```
35+
Input: s = "mxz", k = 3
36+
37+
Output: "i"
38+
39+
Explanation:
40+
41+
The only substring: "mxz", 12 + 23 + 25 = 60, 60 % 26 = 8, result[0] = 'i'.
42+
```
2643

2744
## 结语
2845

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "bytes"
4+
5+
func Solution(s string, k int) string {
6+
buf := bytes.NewBuffer([]byte{})
7+
sum := 0
8+
for i := 0; i < len(s); i++ {
9+
if i != 0 && i%k == 0 {
10+
buf.WriteByte(byte(sum%26 + 'a'))
11+
sum = int(s[i]-'a')
12+
continue
13+
}
14+
sum += int(s[i] - 'a')
15+
}
16+
buf.WriteByte(byte(sum%26 + 'a'))
17+
return buf.String()
518
}

leetcode/3201-3300/3271.Hash-Divided-String/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
k int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abcd", 2, "bf"},
18+
{"TestCase2", "mxz", 3, "i"},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)