Skip to content

Commit 40f8376

Browse files
authored
Create 2138-divide-a-string-into-groups-of-size-k.js
1 parent 903cc8d commit 40f8376

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @param {character} fill
5+
* @return {string[]}
6+
*/
7+
var divideString = function(s, k, fill) {
8+
let res = [], tmp = ''
9+
for(let i = 0, n = s.length; i < n; i++) {
10+
tmp += s[i]
11+
if(tmp.length === k) {
12+
res.push(tmp)
13+
tmp = ''
14+
}
15+
}
16+
if(tmp.length) {
17+
for(let i = 0, limit = k - tmp.length; i < limit; i++) {
18+
tmp += fill
19+
}
20+
res.push(tmp)
21+
}
22+
return res
23+
};

0 commit comments

Comments
 (0)