Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String licenseKeyFormatting(String S, int K) {
- StringBuilder sb = new StringBuilder(), sb2 = new StringBuilder();
- for(char ch : S.toCharArray())
- {
- if(ch == '-') continue;
- if(Character.isLowerCase(ch)) ch = Character.toUpperCase(ch);
- sb.append(ch);
- }
- int n = sb.length();
- for(int i = n - 1, count = 0; i >= 0; i--)
- {
- sb2.append(sb.charAt(i));
- count++;
- if(count % K == 0)
- {
- sb2.append('-');
- count = 0;
- }
- }
- n = sb2.length();
- if(n > 1 && sb2.charAt(n - 1) == '-') sb2.deleteCharAt(n - 1);
- return sb2.reverse().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement