C++ solution using if-else if-else statement


  • 0
    M
    class Solution {
    public:
        string licenseKeyFormatting(string S, int K) {//"2-4A0r7-4k"
            int str_size = S.length();
            string result;
            char temp;
            int count = K;
            for(int i = str_size - 1; i >= 0; --i){
                temp = S[i];
                if(temp == '-'){
                    continue;
                }else if(count == 0){
                    result.push_back('-');
                    count = K;
                    if(temp >= 'a' && temp <= 'z'){
                        temp = toupper(temp);
                        result.push_back(temp);
                        count--;
                    }else{
                        result.push_back(temp);
                        count--;
                    }
                }else if(temp >= 'a' && temp <= 'z'){
                    temp = toupper(temp);
                    result.push_back(temp);
                    count--;
                }else{
                    result.push_back(temp);
                    count--;
                }
            }
            reverse(result.begin(), result.end());
            return result;
        }
    };
    

Log in to reply
 

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