Divide a String Into Groups of Size k - Problem

Imagine you're a librarian organizing books into boxes of equal size! Given a string s, you need to divide it into groups of exactly k characters.

Here's how the process works:

  • ๐Ÿ“ฆ The first group gets the first k characters
  • ๐Ÿ“ฆ The second group gets the next k characters
  • ๐Ÿ“ฆ And so on...

But what if the last group doesn't have enough characters? No problem! We use a special fill character to pad it to size k.

Goal: Return an array of strings where each string represents one group of size k.

Example: If s = "abcdefg", k = 3, and fill = 'x', we get ["abc", "def", "gxx"]

Input & Output

example_1.py โ€” Basic Division
$ Input: s = "abcdefghi", k = 3, fill = "x"
โ€บ Output: ["abc", "def", "ghi"]
๐Ÿ’ก Note: The string has exactly 9 characters, which divides perfectly into 3 groups of size 3. No fill character is needed since each group has exactly k characters.
example_2.py โ€” Padding Required
$ Input: s = "abcdefg", k = 3, fill = "x"
โ€บ Output: ["abc", "def", "gxx"]
๐Ÿ’ก Note: The string has 7 characters. The first two groups get 3 characters each: "abc" and "def". The last group only has 1 character "g", so we pad it with 2 'x' characters to make "gxx".
example_3.py โ€” Single Character
$ Input: s = "a", k = 5, fill = "*"
โ€บ Output: ["a****"]
๐Ÿ’ก Note: Edge case: The string has only 1 character but k=5. We need to create one group with the single character 'a' followed by 4 fill characters '*' to reach the required size of 5.

Visualization

Tap to expand
๐Ÿ“š String Division: Like Organizing a LibraryBook Shelf: "abcdefg"abcdefgBox 1 (k=3)abcBox 2 (k=3)defBox 3 (k=3)gxxResult: ["abc", "def", "gxx"]๐ŸŽฏ Key Insight: Fill incomplete boxes with dummy items๐Ÿ’ก Optimization StrategyProcess in chunks of size k using string slicing: O(n) time complexityOnly the last group needs special padding logic - keeps code simple!
Understanding the Visualization
1
Survey the Shelf
Look at your string of characters - these are your 'books' to organize
2
Prepare Boxes
Each box (group) must hold exactly k books (characters)
3
Fill Boxes in Order
Take books from left to right, filling each box completely
4
Add Dummy Books
If the last box isn't full, add dummy books (fill character) to complete it
Key Takeaway
๐ŸŽฏ Key Insight: This problem is essentially about string partitioning with padding. The optimal solution processes the string in chunks of size k, making it both efficient and intuitive to understand.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We visit each character exactly once, and string slicing is O(k) for each group

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

We store the result array, which contains all characters from the input plus any fill characters

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • s consists of lowercase English letters only
  • 1 โ‰ค k โ‰ค 100
  • fill is a lowercase English letter
  • The last group will be padded with fill character if needed
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 15
23.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen