Skip to content

Commit c613b6b

Browse files
authored
Merge pull request #1241 from 0xff-dev/2138
Add solution and test-cases for problem 2138
2 parents 82334d1 + 6755057 commit c613b6b

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2138.Divide a String Into Groups of Size k][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
74

5+
A string `s` can be partitioned into groups of size `k` using the following procedure:
6+
7+
- The first group consists of the first `k` characters of the string, the second group consists of the next k characters of the string, and so on. Each element can be a part of **exactly one** group.
8+
- For the last group, if the string **does not** have `k` characters remaining, a character `fill` is used to complete the group.
9+
10+
Note that the partition is done so that after removing the `fill` character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be `s`.
11+
12+
Given the string `s`, the size of each group `k` and the character `fill`, return a string array denoting the **composition of every group** `s` has been divided into, using the above procedure.
13+
814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: s = "abcdefghi", k = 3, fill = "x"
18+
Output: ["abc","def","ghi"]
19+
Explanation:
20+
The first 3 characters "abc" form the first group.
21+
The next 3 characters "def" form the second group.
22+
The last 3 characters "ghi" form the third group.
23+
Since all groups can be completely filled by characters from the string, we do not need to use fill.
24+
Thus, the groups formed are "abc", "def", and "ghi".
1325
```
1426

15-
## 题意
16-
> ...
17-
18-
## 题解
27+
**Example 2:**
1928

20-
### 思路1
21-
> ...
22-
Divide a String Into Groups of Size k
23-
```go
2429
```
25-
30+
Input: s = "abcdefghij", k = 3, fill = "x"
31+
Output: ["abc","def","ghi","jxx"]
32+
Explanation:
33+
Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
34+
For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
35+
Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, k int, fill byte) []string {
4+
l := len(s)
5+
bs := []byte(s)
6+
mod := l % k
7+
if mod != 0 {
8+
for i := 0; i < k-mod; i++ {
9+
bs = append(bs, fill)
10+
}
11+
}
12+
var ans []string
13+
for i := 0; i < len(bs); i += k {
14+
ans = append(ans, string(bs[i:i+k]))
15+
}
16+
return ans
517
}

leetcode/2101-2200/2138.Divide-a-String-Into-Groups-of-Size-k/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ 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+
fill byte
16+
expect []string
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", "abcdefghi", 3, 'x', []string{"abc", "def", "ghi"}},
19+
{"TestCase2", "abcdefghij", 3, 'x', []string{"abc", "def", "ghi", "jxx"}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.k, c.fill)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.inputs, c.k, c.fill)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)