forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_482.java
30 lines (29 loc) · 1.12 KB
/
_482.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
28
29
30
package com.fishercoder.solutions;
public class _482 {
public static class Solution1 {
public String licenseKeyFormatting(String S, int K) {
StringBuilder stringBuilder = new StringBuilder();
char[] SChars = S.toCharArray();
for (int i = S.length() - 1, j = 0; i >= 0; ) {
if (j < K) {
if (SChars[i] != '-') {
if (SChars[i] >= 'a' && SChars[i] <= 'z') {
stringBuilder.append(Character.toUpperCase(SChars[i]));
} else {
stringBuilder.append(SChars[i]);
}
j++;
}
i--;
} else if (j == K) {
j = 0;
stringBuilder.append('-');
}
}
if (stringBuilder.length() > 1 && stringBuilder.substring(stringBuilder.length() - 1).equals("-")) {
return stringBuilder.reverse().substring(1);
}
return stringBuilder.reverse().toString();
}
}
}