-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicenseKeyFormatting.java
More file actions
37 lines (34 loc) · 1.08 KB
/
LicenseKeyFormatting.java
File metadata and controls
37 lines (34 loc) · 1.08 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
package leetcode;
/**
* LicenseKeyFormatting
* https://leetcode-cn.com/problems/license-key-formatting/
* 482. 密钥格式化
* https://leetcode-cn.com/problems/license-key-formatting/solution/cong-hou-wang-qian-bian-li-by-oshdyr-dmzj/
*
* @author tobin
* @since 2021-10-04
*/
public class LicenseKeyFormatting {
public static void main(String[] args) {
LicenseKeyFormatting sol = new LicenseKeyFormatting();
System.out.println(sol.licenseKeyFormatting("2-5g-3-J", 2));
}
public String licenseKeyFormatting(String s, int k) {
StringBuilder resultSB = new StringBuilder();
String upper_s = s.toUpperCase(); // BUG 1
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = upper_s.charAt(i);
if (c == '-') {
continue;
}
if (resultSB.length() > 0 && count % k == 0) {
resultSB.append('-');
count = 0;
}
resultSB.append(c);
count++;
}
return resultSB.reverse().toString();
}
}