Java Beats 100 % StringBuilder


  • 0
    M
    public class Solution {
        public String licenseKeyFormatting(String s, int k) {
            StringBuilder sb=new StringBuilder();
            for(char c: s.toCharArray()){
                if(c=='-')continue;
                if((c-'A')>25) c=(char)(c-32);
                sb.append(c);
            }
            int j=0;
            for(int i=sb.length()-1;i>0;i--){
                if(++j%k==0)sb.insert(i,"-");
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.