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 groupshas been divided into, using the above procedure.
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".
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".
The problem is about splitting the string into chunks of size k. If the last chunk is smaller than k, we pad it with the fill character. This ensures all groups are of equal size, and reconstructing the string (removing fills) gives the original.
classSolution {
public: vector<string> divideString(string s, int k, char fill) {
vector<string> ans;
for (int i =0; i < s.size(); i += k) {
string group = s.substr(i, k);
if (group.size() < k) group += string(k - group.size(), fill);
ans.push_back(group);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcdivideString(sstring, kint, fillbyte) []string {
varans []stringfori:=0; i < len(s); i+=k {
end:=i+kgroup:=""ifend<= len(s) {
group = s[i:end]
} else {
group = s[i:]
for len(group) < k {
group+= string(fill)
}
}
ans = append(ans, group)
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
public String[]divideString(String s, int k, char fill) {
List<String> ans =new ArrayList<>();
for (int i = 0; i < s.length(); i += k) {
String group;
if (i + k <= s.length()) {
group = s.substring(i, i + k);
} else {
group = s.substring(i);
while (group.length() < k) {
group += fill;
}
}
ans.add(group);
}
return ans.toArray(new String[0]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
fundivideString(s: String, k: Int, fill: Char): List<String> {
val ans = mutableListOf<String>()
var i = 0while (i < s.length) {
var group = if (i + k <= s.length) s.substring(i, i + k) else s.substring(i)
if (group.length < k) group += fill.toString().repeat(k - group.length)
ans.add(group)
i += k
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defdivideString(self, s: str, k: int, fill: str) -> list[str]:
ans: list[str] = []
for i in range(0, len(s), k):
group = s[i:i+k]
if len(group) < k:
group += fill * (k - len(group))
ans.append(group)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfndivide_string(s: String, k: i32, fill: char) -> Vec<String> {
letmut ans = Vec::new();
let k = k asusize;
let chars: Vec<char>= s.chars().collect();
let n = chars.len();
letmut i =0;
while i < n {
letmut group: String = chars[i..std::cmp::min(i + k, n)].iter().collect();
if group.len() < k {
group.extend(std::iter::repeat(fill).take(k - group.len()));
}
ans.push(group);
i += k;
}
ans
}
}