A string s can be partitioned into groups of size k using the following procedure:
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.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 <= 100s consists of lowercase English letters only.1 <= k <= 100fill is a lowercase English letter.This approach involves iterating through the string in increments of k and forming each group by taking k characters. If the number of characters left is less than k and a fill character is required, we append the fill character accordingly to complete the group.
The C code utilizes the printf function to extract substrings of size k from the input string s. It uses the integer division method to determine how many full groups are required. For the remainder of the string that does not fill an entire group, the fill character is appended until the group is complete.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the length of the string s.
Space Complexity: O(1), as no extra space other than a few variables is used.
This approach builds on modern language features such as list comprehensions and string formatting to achieve a succinct solution. By leveraging these tools, we can split the string into fixed-sized parts and adjust any part that doesn't meet the required size through padding.
In this Python solution, a list comprehension generates each group. The slice is padded with fill charaacters, but only up to a length of k, to ensure that every group is the correct size.
JavaScript
Time Complexity: O(n), where n is the length of the string s.
Space Complexity: O(n/k), due to list storage.
| Approach | Complexity |
|---|---|
| Using Loop and Substring | Time Complexity: O(n), where n is the length of the string s. |
| Using List Comprehension and Padding | Time Complexity: O(n), where n is the length of the string s. |
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,224 views views
Watch 9 more video solutions →Practice Divide a String Into Groups of Size k with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor