2138. Divide a String Into Groups of Size k LeetCode Solution
In this guide, you will get 2138. Divide a String Into Groups of Size k LeetCode Solution with the best time and space complexity. The solution to Divide a String Into Groups of Size k problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Divide a String Into Groups of Size k solution in C++
- Divide a String Into Groups of Size k solution in Java
- Divide a String Into Groups of Size k solution in Python
- Additional Resources

Problem Statement of 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 character 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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2138. Divide a String Into Groups of Size k LeetCode Solution in C++
class Solution {
public:
vector<string> divideString(string s, int k, char fill) {
vector<string> ans;
for (int i = 0; i < s.length(); i += k)
ans.push_back(i + k > s.length()
? s.substr(i) + string(i + k - s.length(), fill)
: s.substr(i, k));
return ans;
}
};
/* code provided by PROGIEZ */
2138. Divide a String Into Groups of Size k LeetCode Solution in Java
class Solution {
public String[] divideString(String s, int k, char fill) {
String[] ans = new String[(s.length() + k - 1) / k];
for (int i = 0, j = 0; i < s.length(); i += k)
ans[j++] = i + k > s.length()
? s.substring(i) + String.valueOf(fill).repeat(i + k - s.length())
: s.substring(i, i + k);
return ans;
}
}
// code provided by PROGIEZ
2138. Divide a String Into Groups of Size k LeetCode Solution in Python
class Solution:
def divideString(self, s: str, k: int, fill: str) -> list[str]:
return [
s[i:] + fill * (i + k - len(s)) if i + k > len(s)
else s[i:i + k]
for i in range(0, len(s), k)
]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.