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
Inputs = "abcdefghi", k = 3, fill = "x"
Output["abc","def","ghi"]
The string is divided into complete groups of size 3, so no fill character is needed.
Example 2
Inputs = "abcdefghij", k = 3, fill = "x"
Output["abc","def","ghi","jxx"]
The first three groups are complete, and the last group uses 'j' plus two fill characters to reach size 3.

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate