Simple Java Solution


  • 0
    M
        public String licenseKeyFormatting(String S, int K) {
            StringBuffer sb = new StringBuffer();
    		int cnt = 0;
    		for (int i = S.length() - 1; i >= 0; --i) {
    			if (S.charAt(i) == '-') continue;
    			if (cnt > 0 && (cnt % K) == 0) sb.insert(0, '-');
    			sb.insert(0, Character.toUpperCase(S.charAt(i)));
    			++cnt;
    		}
    		return sb.toString();
        }
    

Log in to reply
 

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