Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

License Key Formatting


Solution


    class Solution {
    public:
        string licenseKeyFormatting(string S, int K) {
            string ans = "";
            int count = 0;
            string temp = "";
            for(int i = S.length() - 1; i >= 0; i--) {
                if(S[i] != '-')
                    count++;
            }
            int i = 0;
            int added = 0;
            for(; i < S.length(); i++) {
                if(S[i] == '-')
                    continue;
                else if(S[i] >= 'a' && S[i] <= 'z') 
                    ans += (char)(S[i] + 'A' - 'a');
                else
                    ans += S[i];
                added++;
                if(added == count % K || added == K)
                    break;
            }
            added = 0;
            i++;
            for(; i < S.length(); i++) {
                if(S[i] == '-')
                    continue;
                else if(S[i] >= 'a' && S[i] <= 'z') 
                    temp += (char)(S[i] + 'A' - 'a');
                else
                    temp += S[i];
                added++;
                if(added == K) {
                    ans += '-' + temp;
                    temp = "";
                    added = 0;
                }
            }
            return ans;
        }
    };