2138.js
/** ------------------------------------------------------------------------------
*
* 2138. Divide a String Into Groups of Size k
* Topics: String
*
* https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/description/?envType=daily-question&envId=2025-06-22
*
------------------------------------------------------------------------------ */
var divideString = function (s, k, fill) {
const res = [] // grouped string
const n = s.length
let curr = 0 // starting index of each group
// split string
while (curr < n) {
const end = Math.min(curr + k, n)
res.push(s.slice(curr, end))
curr += k
}
// try to fill in the last group
const last = res[res.length - 1]
if (last.length < k) {
res[res.length - 1] = last + fill.repeat(k - last.length)
}
return res
}
console.log()