-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicenseKeyFormatting.java
More file actions
45 lines (35 loc) · 1.17 KB
/
Copy pathLicenseKeyFormatting.java
File metadata and controls
45 lines (35 loc) · 1.17 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
39
40
41
42
43
44
package LeetcodePractice;
public class LicenseKeyFormatting {
public static void main(String[] args) {
String s1 = "5F3Z-2e-9-w";
int k1 = 4;
System.out.println(licenseKeyFormatting(s1, k1));
String s2 = "2-5g-3-J";
int k2 = 2;
System.out.println(licenseKeyFormatting(s2, k2));
}
public static String licenseKeyFormatting(String s, int k) {
StringBuilder cleaned = new StringBuilder();
for (char c : s.toCharArray()) {
if (c != '-') {
cleaned.append(Character.toUpperCase(c));
}
}
StringBuilder result = new StringBuilder();
int length = cleaned.length();
int firstGroupLength = length % k;
int index = 0;
if (firstGroupLength > 0) {
result.append(cleaned.substring(0, firstGroupLength));
index = firstGroupLength;
}
while (index < length) {
if (result.length() > 0) {
result.append('-');
}
result.append(cleaned.substring(index, index + k));
index += k;
}
return result.toString();
}
}