[LeetCode] 2138. Divide a String Into Groups of Size k

A string s can be partitioned into groups of size k using the following procedure:

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.
For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
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.

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.

Example 1:
Input: s = “abcdefghi”, k = 3, fill = “x”
Output: [“abc”,”def”,”ghi”]
Explanation:
The first 3 characters “abc” form the first group.
The next 3 characters “def” form the second group.
The last 3 characters “ghi” form the third group.
Since all groups can be completely filled by characters from the string, we do not need to use fill.
Thus, the groups formed are “abc”, “def”, and “ghi”.

Example 2:
Input: s = “abcdefghij”, k = 3, fill = “x”
Output: [“abc”,”def”,”ghi”,”jxx”]
Explanation:
Similar to the previous example, we are forming the first three groups “abc”, “def”, and “ghi”.
For the last group, we can only use the character ‘j’ from the string. To complete this group, we add ‘x’ twice.
Thus, the 4 groups formed are “abc”, “def”, “ghi”, and “jxx”.

Constraints:
1 <= s.length <= 100
s consists of lowercase English letters only.
1 <= k <= 100
fill is a lowercase English letter.

将字符串拆分为若干长度为 k 的组.

字符串 s 可以按下述步骤划分为若干长度为 k 的组:

第一组由字符串中的前 k 个字符组成,第二组由接下来的 k 个字符串组成,依此类推。每个字符都能够成为 某一个 组的一部分。
对于最后一组,如果字符串剩下的字符 不足 k 个,需使用字符 fill 来补全这一组字符。
注意,在去除最后一个组的填充字符 fill(如果存在的话)并按顺序连接所有的组后,所得到的字符串应该是 s 。

给你一个字符串 s ,以及每组的长度 k 和一个用于填充的字符 fill ,按上述步骤处理之后,返回一个字符串数组,该数组表示 s 分组后 每个组的组成情况 。

思路

这道题就是模拟。将字符串分成若干个长度为 k 的组,最后一组如果不足 k 个字符,则用 fill 填充。可以使用 StringBuilder 来构建每个组。

复杂度

时间O(n)
空间O(n)

代码

Java实现一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public String[] divideString(String s, int k, char fill) {
int n = s.length();
int groups = (n + k - 1) / k; // Calculate the number of groups needed
String[] res = new String[groups];
for (int i = 0; i < groups; i++) {
int start = i * k;
int end = Math.min(start + k, n);
StringBuilder sb = new StringBuilder(s.substring(start, end));
// If the group is not full, fill it with the fill character
while (sb.length() < k) {
sb.append(fill);
}
res[i] = sb.toString();
}
return res;
}
}

Java实现二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public String[] divideString(String s, int k, char fill) {
int n = s.length();
List<String> list = new ArrayList<>();
int i = 0;
while (i < n) {
String str = s.substring(i, Math.min(i + k, s.length()));
if (str.length() == k) {
list.add(str);
} else {
int diff = k - str.length();
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
sb.append(c);
}
for (int j = 0; j < diff; j++) {
sb.append(fill);
}
list.add(sb.toString());
}
i += k;
}

String[] res = new String[list.size()];
for (int j = 0; j < list.size(); j++) {
res[j] = list.get(j);
}
return res;
}
}

[LeetCode] 2138. Divide a String Into Groups of Size k
https://shurui91.github.io/posts/3233707564.html
Author
Aaron Liu
Posted on
June 21, 2025
Licensed under