forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2138.java
27 lines (26 loc) · 876 Bytes
/
_2138.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions;
public class _2138 {
public static class Solution1 {
public String[] divideString(String s, int k, char fill) {
int len = s.length() / k;
if (s.length() % k != 0) {
len++;
}
String[] ans = new String[len];
for (int i = 0, j = 0; i < s.length(); i += k, j++) {
if (j == len - 1) {
StringBuilder sb = new StringBuilder(s.substring(i));
if (sb.length() != k) {
while (sb.length() < k) {
sb.append(fill);
}
}
ans[j] = sb.toString();
} else {
ans[j] = s.substring(i, i + k);
}
}
return ans;
}
}
}