g0401_0500.s0482_license_key_formatting.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0401_0500.s0482_license_key_formatting;
// #Easy #String #2022_07_20_Time_8_ms_(96.52%)_Space_45.4_MB_(58.75%)
public class Solution {
public String licenseKeyFormatting(String s, int k) {
StringBuilder sb = new StringBuilder();
int cnt = 0;
int occ = 0;
for (char c : s.toCharArray()) {
if (c == '-') {
continue;
}
cnt++;
}
int l = cnt % k;
for (char c : s.toCharArray()) {
if (c == '-') {
continue;
}
if (occ == k) {
occ = 0;
sb.append('-');
} else if (occ == l && l != 0) {
l = 0;
occ = 0;
sb.append('-');
}
sb.append(Character.toUpperCase(c));
occ++;
}
return sb.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy