simple java solution


  • 0
    S
    public class Solution {
        public String licenseKeyFormatting(String S, int K) {
            S = S.replace("-", "").toUpperCase();
            int len = S.length();
            StringBuilder sb = new StringBuilder();
            int res = len, i = len-1;
            while(res-K > 0){
                res -= K;
                for(int j = 0; j < K; j++){
                    sb.append(S.charAt(i--));
                }
                sb.append("-");
            }
            for(int j = 0; j < res; j++){
                sb.append(S.charAt(i--));
            }
            return sb.reverse().toString();
        }
    }
    
    

Log in to reply
 

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