LeetCode Problem Workspace

Divide a String Into Groups of Size k

Divide a string into equal groups of size k, using a fill character to complete the last group if needed.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · String plus Simulation

bolt

Answer-first summary

Divide a string into equal groups of size k, using a fill character to complete the last group if needed.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for String plus Simulation

Try AiBox Copilotarrow_forward

To solve this problem, iterate through the string in steps of size k, slicing each group directly from the original string. If the last group is smaller than k, append the fill character until it reaches size k. This approach leverages a direct simulation of the grouping process, avoiding unnecessary extra structures and ensuring the output order preserves the original string.

Problem Statement

Given a string s, an integer k, and a character fill, divide s into groups of size k. Each group should contain exactly k characters. If the last group is smaller than k, append the fill character until it reaches the required size.

Return a string array representing the groups. The concatenation of all groups, after removing any fill characters, should reconstruct the original string s. Ensure the order of characters in s is maintained across all groups.

Examples

Example 1

Input: s = "abcdefghi", k = 3, fill = "x"

Output: ["abc","def","ghi"]

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"]

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.

Solution Approach

Iterate and Slice

Loop through the string from index 0 to the end with step k. For each iteration, slice a substring of length k and append it to the result array.

Handle Last Group

If the last sliced substring is shorter than k, repeatedly append the fill character until it reaches length k. This ensures all groups are uniform in size.

Return Result

After processing all segments, return the array of strings. The array directly represents the groups, with the last group padded as needed, preserving the original string order.

Complexity Analysis

Metric Value
Time O(\max(n, k))
Space O(1)

Time complexity is O(max(n, k)) because we process each character of s once and may append up to k fill characters. Space complexity is O(1) extra space beyond the output array.

What Interviewers Usually Probe

  • Can you simulate the grouping without building intermediate arrays unnecessarily?
  • What happens if the last group is smaller than k?
  • How does your solution ensure the original string can be reconstructed from the groups?

Common Pitfalls or Variants

Common pitfalls

  • Not padding the last group correctly, leading to inconsistent group sizes.
  • Altering the order of characters, which breaks the reconstruction requirement.
  • Overcomplicating with additional data structures instead of direct slicing.

Follow-up variants

  • Use a dynamic fill character based on position or group index.
  • Divide strings into groups where group sizes follow a repeating pattern rather than a fixed k.
  • Return groups without any fill, allowing the last group to be shorter than k.

FAQ

What is the main pattern used in Divide a String Into Groups of Size k?

The problem uses a String plus Simulation pattern where you iterate and slice substrings, simulating the grouping process.

Do I always need to add fill characters?

No, fill characters are only appended if the last group has fewer than k characters to ensure uniform group sizes.

Can I reconstruct the original string from the groups?

Yes, concatenating all groups after removing any fill characters will reconstruct the original string.

What is the time complexity of this approach?

The time complexity is O(max(n, k)) because each character is processed once and the last group may require up to k fill additions.

Is extra space required besides the output array?

No significant extra space is needed beyond storing the resulting array of groups.

terminal

Solution

Solution 1: Simulation

We can directly simulate the process described in the problem statement, dividing the string $s$ into groups of length $k$. For the last group, if it contains fewer than $k$ characters, we use the character $\textit{fill}$ to pad it.

1
2
3
class Solution:
    def divideString(self, s: str, k: int, fill: str) -> List[str]:
        return [s[i : i + k].ljust(k, fill) for i in range(0, len(s), k)]
Divide a String Into Groups of Size k Solution: String plus Simulation | LeetCode #2138 Easy