-
Notifications
You must be signed in to change notification settings - Fork 1
wstcliyu/LeetCodeJava
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
public class License_482 {
// My first solution
/*
public String licenseKeyFormatting(String S, int K) {
StringBuilder res = new StringBuilder();
StringBuilder tmp = new StringBuilder();
for (int i=S.length()-1; i>=0; i--) {
char c = S.charAt(i);
if (c == '-')
continue;
if (Character.isLetter(c))
c = Character.toUpperCase(c);
tmp.insert(0, c);
if (tmp.length() == K) {
res.insert(0, "-" + tmp);
tmp = new StringBuilder();
}
}
if (tmp.length() != 0)
return res.insert(0, tmp).toString();
return res.length() == 0 ? "" : res.deleteCharAt(0).toString();
}
*/
// Most voted solution
public String licenseKeyFormatting(String S, int K) {
StringBuilder sb = new StringBuilder();
for (int i=S.length()-1; i>=0; i--) {
if (S.charAt(i) != '-')
sb.append(sb.length() % (K + 1) == K ? "-" : "").append(S.charAt(i));
}
return sb.reverse().toString().toUpperCase();
}
}
About
No description, website, or topics provided.
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published