C++ Simple Solution


  • 0
    class Solution {
    public:
        string licenseKeyFormatting(string S, int K) {
            if (K <= 0)
                return "";
            string res, tmp;
            for (int i = S.size() - 1; i >= 0; i--) {
                char c = S[i];
                if (c != '-') {
                    tmp += toupper(c);
                }
                if (tmp.size() == K) {
                    res += "-" + tmp;
                    tmp = "";
                }
            }
            if (tmp.size())
                res += "-" + tmp;
            if (res.size())
                res = res.substr(1);
            reverse(res.begin(), res.end());
            return res;
        }
    };
    

Log in to reply
 

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