Divide a String Into Groups of Size k

Easy

Description

Divide a string s into groups of size k, and if the last group is smaller than k, pad it with a given fill character.

Examples

Input:s = "abcdefghi", k = 3, fill = "x"
Output:["abc","def","ghi"]
Explanation:

Groups of 3.

Input:s = "hello", k = 2, fill = "z"
Output:["he","ll","oz"]
Explanation:

The string "hello" is divided into groups of size 2. First group: "he", second group: "ll", third group: "o" (incomplete). The last group is padded with one 'z' to make it "oz".

Input:s = "abc", k = 5, fill = "*"
Output:["abc**"]
Explanation:

The string "abc" has length 3 with k=5, so only one group is needed. Since the group is incomplete (3 < 5), it is padded with 2 fill characters '*' to reach size 5, resulting in "abc**".

Constraints

  • 1 ≤ s.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!