Created
June 22, 2025 16:11
-
-
Save tatsuyax25/b7469e2187eb2bf4a292070e0bb52465 to your computer and use it in GitHub Desktop.
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 p
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} s | |
* @param {number} k | |
* @param {character} fill | |
* @return {string[]} | |
*/ | |
var divideString = function(s, k, fill) { | |
const result = []; | |
// Loop through the string in increments of k | |
for (let i = 0; i < s.length; i += k) { | |
// Get a substring of length k starting from index i | |
let group = s.slice(i, i + k); | |
// If the group is shorter than k, pad it with the fill character | |
if (group.length < k) { | |
group = group.padEnd(k, fill); | |
} | |
// Add the group to the result array | |
result.push(group); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment