Java 3 Lines


  • 0
        public String licenseKeyFormatting(String str, int k) {
            StringBuilder sb = new StringBuilder(str.toUpperCase().replaceAll("-",""));
            for(int p = sb.length()-k; p>0; p-=k) sb.insert(p, "-");
            return sb.toString(); 
        }
    

Log in to reply
 

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