-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2138.java
More file actions
38 lines (32 loc) · 1.77 KB
/
Copy pathLC2138.java
File metadata and controls
38 lines (32 loc) · 1.77 KB
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
28
29
30
31
32
33
34
35
36
37
38
/*
* 2138. Divide a String Into Groups of Size k
*
* 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 part of * exactly one group.
* For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
* Note that the partition is done so that after removing the fill character from the last group (if it exists) and concatenating all the groups in order, the resultant string * should be s.
* Given the string s, the size of each group k and the character fill, return a string array denoting the composition of every group s has been divided into, using the above * procedure.
*/
class Solution {
public String[] divideString(String s, int k, char fill) {
int sl = s.length();
int sizeOfOutputStringArray = (sl+k-1) / k;
// System.out.println(sl);
// System.out.println(sizeOfOutputStringArray);
String[] result = new String[sizeOfOutputStringArray];
for (int start = 0, end = k, index = 0;
index < sizeOfOutputStringArray;
start += k, end += k, index++) {
// System.out.println(start + "\t" + end + "\t" + index);
if (end <= sl) {
result[index] = s.substring(start, end);
// System.out.println(result[index]);
} else {
String leftover = String.valueOf(fill).repeat(k - (sl%k));
result[index] = s.substring(start).concat(leftover);;
// System.out.println("Leftover: " + result[index]);
}
}
return result;
}
}