# 482 License Key Formatting

Input:
 S = "2-4A0r7-4k", K = 3


Output:
 "24-A0R-74K"


Explanation:
 The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said above.
public class Solution {
    public String licenseKeyFormatting(String S, int K) {
        /*
        concept:
        convert all character in S to uppercase
        split S by "-"
        combine all components of S, form new s
        create a stringbuilder sb
        for each character in s, read from the end of s, decrease index by K
            add substring of length K to sb, add a dash to sb
        */
        /*
        S = "2-4A0r7-4k", K = 3
        S = "2-4A0R7-4K"
        cArray = {2,4A0R7,4K}
        s=24A0R74K
        i=8,i-K=5
        pseudo:
        S = S.toUpperCase()
        char[] cArray = S.split("-");
        StringBuilder sb;
        for each element in cArray
            sb.append(cArray[i])
        String s = sb.toString();
        sb.clear()
        for(int i=s.length(); i>=0; i-=K;){
            sb.insert(0,s.substring(i-K,i)).insert(0,"-")

        return sb.toString()
        */

        S=S.toUpperCase();
        String[] cArray = S.split("-");
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<cArray.length; i++){
            sb.append(cArray[i]);
        }
        String s = sb.toString();
        sb.setLength(0);
        for(int i=s.length(); i>=0; i-=K){
            if(i-K>0)
                sb.insert(0,s.substring(i-K,i)).insert(0,"-");
            else
                sb.insert(0,s.substring(0,i));
        }
        return sb.toString();
    }
}

results matching ""

    No results matching ""