|
1 | 1 | # [2138.Divide a String Into Groups of Size k][title]
|
2 | 2 |
|
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 |
| -
|
6 | 3 | ## Description
|
7 | 4 |
|
| 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 | + |
8 | 14 | **Example 1:**
|
9 | 15 |
|
10 | 16 | ```
|
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". |
13 | 25 | ```
|
14 | 26 |
|
15 |
| -## 题意 |
16 |
| -> ... |
17 |
| -
|
18 |
| -## 题解 |
| 27 | +**Example 2:** |
19 | 28 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Divide a String Into Groups of Size k |
23 |
| -```go |
24 | 29 | ```
|
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 | +``` |
26 | 37 |
|
27 | 38 | ## 结语
|
28 | 39 |
|
|
0 commit comments