C++ one pass,


  • 0
    R
    class Solution {
    public:
        string licenseKeyFormatting(string S, int K) {
            string res;
            int ind = S.size()-1, len = 0;
            if(ind<0) return S;
            while(ind >= 0){
                char c = S[ind];
                if(c == '-'){
                    ind--;
                    continue;
                }else{
                    len++;
                    res.push_back(islower(c)?toupper(c):c );
                    if(len == K){
                        len = 0 ;
                        res.push_back('-');
                    }
                    ind--;
                }
            }
            reverse(res.begin(), res.end());
            return res[0]=='-'?res.substr(1):res;
        }
    };
    

Log in to reply
 

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