License Key Formatting LeetCode solution in Java

1
class Solution {
2
public String licenseKeyFormatting(String s, int k) {
3
StringBuilder answer = new StringBuilder();
4
int length = 0;
5
// Iterate Backwards to fullfill first group condition
6
for (int i = s.length() - 1; i >= 0; i--) {
7
if (s.charAt(i) == '-') {
8
continue;
9
}
10
if (length > 0 && length % k == 0) {
11
answer.append('-');
12
}
13
answer.append(Character.toUpperCase(s.charAt(i)));
14
length++;
15
}
16
return answer.reverse().toString();
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0