Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 element 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.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abcdefghi" 3 "x"
"abcdefghij" 3 "x"
text-justification)positions-of-large-groups)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2138: Divide a String Into Groups of Size k
class Solution {
public String[] divideString(String s, int k, char fill) {
int n = s.length();
String[] ans = new String[(n + k - 1) / k];
if (n % k != 0) {
s += String.valueOf(fill).repeat(k - n % k);
}
for (int i = 0; i < ans.length; ++i) {
ans[i] = s.substring(i * k, (i + 1) * k);
}
return ans;
}
}
// Accepted solution for LeetCode #2138: Divide a String Into Groups of Size k
func divideString(s string, k int, fill byte) (ans []string) {
n := len(s)
if n%k != 0 {
s += strings.Repeat(string(fill), k-n%k)
}
for i := 0; i < len(s)/k; i++ {
ans = append(ans, s[i*k:(i+1)*k])
}
return
}
# Accepted solution for LeetCode #2138: Divide a String Into Groups of Size k
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)]
// Accepted solution for LeetCode #2138: Divide a String Into Groups of Size k
struct Solution;
impl Solution {
pub fn divide_string(s: String, k: i32, fill: char) -> Vec<String> {
let k = k as usize;
let mut answer = s.chars().collect::<Vec<char>>();
answer.resize(s.len() + (k - s.len() % k) % k, fill);
answer
.chunks(k as usize)
.map(|c| c.iter().collect::<String>())
.collect::<Vec<String>>()
}
}
#[test]
fn test() {
let s = "abcdefghi".to_string();
let k = 3;
let fill = 'x';
let res = vec_string!["abc", "def", "ghi"];
assert_eq!(Solution::divide_string(s, k, fill), res);
let s = "abcdefghij".to_string();
let k = 3;
let fill = 'x';
let res = vec_string!["abc", "def", "ghi", "jxx"];
assert_eq!(Solution::divide_string(s, k, fill), res);
}
// Accepted solution for LeetCode #2138: Divide a String Into Groups of Size k
function divideString(s: string, k: number, fill: string): string[] {
const ans: string[] = [];
for (let i = 0; i < s.length; i += k) {
ans.push(s.slice(i, i + k).padEnd(k, fill));
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.