Skip to content

Commit ec1b489

Browse files
committed
Add solution #2138
1 parent c77d264 commit ec1b489

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,771 LeetCode solutions in JavaScript
1+
# 1,772 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1638,6 +1638,7 @@
16381638
2134|[Minimum Swaps to Group All 1's Together II](./solutions/2134-minimum-swaps-to-group-all-1s-together-ii.js)|Medium|
16391639
2135|[Count Words Obtained After Adding a Letter](./solutions/2135-count-words-obtained-after-adding-a-letter.js)|Medium|
16401640
2136|[Earliest Possible Day of Full Bloom](./solutions/2136-earliest-possible-day-of-full-bloom.js)|Hard|
1641+
2138|[Divide a String Into Groups of Size k](./solutions/2138-divide-a-string-into-groups-of-size-k.js)|Easy|
16411642
2140|[Solving Questions With Brainpower](./solutions/2140-solving-questions-with-brainpower.js)|Medium|
16421643
2145|[Count the Hidden Sequences](./solutions/2145-count-the-hidden-sequences.js)|Medium|
16431644
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 2138. Divide a String Into Groups of Size k
3+
* https://leetcode.com/problems/divide-a-string-into-groups-of-size-k/
4+
* Difficulty: Easy
5+
*
6+
* A string s can be partitioned into groups of size k using the following procedure:
7+
* - The first group consists of the first k characters of the string, the second group consists
8+
* of the next k characters of the string, and so on. Each element can be a part of exactly one
9+
* group.
10+
* - For the last group, if the string does not have k characters remaining, a character fill is
11+
* used to complete the group.
12+
*
13+
* Note that the partition is done so that after removing the fill character from the last group
14+
* (if it exists) and concatenating all the groups in order, the resultant string should be s.
15+
*
16+
* Given the string s, the size of each group k and the character fill, return a string array
17+
* denoting the composition of every group s has been divided into, using the above procedure.
18+
*/
19+
20+
/**
21+
* @param {string} s
22+
* @param {number} k
23+
* @param {character} fill
24+
* @return {string[]}
25+
*/
26+
var divideString = function(s, k, fill) {
27+
const paddedString = s + fill.repeat((k - s.length % k) % k);
28+
const result = [];
29+
30+
for (let i = 0; i < paddedString.length; i += k) {
31+
result.push(paddedString.slice(i, i + k));
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)