DEV Community

Cover image for πŸ¦… Beginner-Friendly Guide "Divide a String Into Groups of Size k" - LeetCode 2138 (C++ | Python | JavaScript)
Om Shree
Om Shree

Posted on

πŸ¦… Beginner-Friendly Guide "Divide a String Into Groups of Size k" - LeetCode 2138 (C++ | Python | JavaScript)

LeetCode 2138 | Easy | String Manipulation


🧠 Problem Summary

You are given:

  • A string s consisting of lowercase English letters
  • An integer k representing the desired group size
  • A character fill to be used if the final group is incomplete

Your task is to:

  • Divide the string into groups of size k
  • If the last group has fewer than k characters, pad it with the fill character

Return a list of all groups.


🀩 Intuition

This is a simple string-slicing problem where we need to:

  1. Iterate through the string in steps of size k
  2. For each step, extract a substring of size k
  3. If fewer than k characters remain at the end, append the required number of fill characters

This ensures all groups are of uniform length.


πŸ“Š C++ Code

class Solution {
 public:
  vector<string> divideString(string s, int k, char fill) {
    vector<string> ans;

    for (int i = 0; i < s.length(); i += k)
      ans.push_back(i + k > s.length()
                        ? s.substr(i) + string(i + k - s.length(), fill)
                        : s.substr(i, k));

    return ans;
  }
};
Enter fullscreen mode Exit fullscreen mode

πŸ“ Key Notes:

  • We check if the remaining characters are fewer than k
  • If yes, we pad using string(length, fill)
  • Time Complexity: O(n) where n = length of string

πŸ’» JavaScript Code

var divideString = function(s, k, fill) {
    const result = [];

    for (let i = 0; i < s.length; i += k) {
        let chunk = s.slice(i, i + k);
        if (chunk.length < k) {
            chunk += fill.repeat(k - chunk.length);
        }
        result.push(chunk);
    }

    return result;
};
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation:

  • Use .slice() to grab groups
  • Pad using .repeat() if needed

🐍 Python Code

class Solution:
    def divideString(self, s: str, k: int, fill: str) -> List[str]:
        result = []
        for i in range(0, len(s), k):
            chunk = s[i:i+k]
            if len(chunk) < k:
                chunk += fill * (k - len(chunk))
            result.append(chunk)
        return result
Enter fullscreen mode Exit fullscreen mode

βœ… Final Thoughts

This problem tests your understanding of string slicing and basic iteration:

  • Slice by k
  • Handle edge cases with padding

It’s a clean, practical problem to get comfortable with loops and substring operations.

Drop a ❀️ if this helped, and follow along for more clean breakdowns and real-world examples!

Happy coding! πŸš€

Top comments (6)

Collapse
 
thedeepseeker profile image
Anna kowoski

Good intution

Collapse
 
om_shree_0709 profile image
Om Shree

thanks anna

Collapse
 
nevodavid profile image
Nevo David

Pretty cool seeing the same trick in all three languages - makes it look simple.

Collapse
 
om_shree_0709 profile image
Om Shree

Thanks Nevo!

Collapse
 
saile_dalil_4c31e175cc82c profile image
Saile Dalil

Hi, great guide thanks for sharing

Collapse
 
om_shree_0709 profile image
Om Shree

Thanks Saile!