Posted on

Description

Submission

class Solution {
public:
    int compress(vector<char>& chars) {
        vector<char> rets;

        char cur = 0;
        int cnt = 1;

        for(char ch: chars) {
            if( cur != ch) {
                if(cur == 0) {
                    cur = ch;
                    continue;
                }
                rets.push_back(cur);
                if(cnt > 1) {
                    string s = to_string(cnt);
                    for(char c: s) {
                        rets.push_back(c);
                    }
                }
                
                cur = ch;
                cnt = 1;
            } else {
                ++cnt;
            }
        }
        string s = to_string(cnt);
        rets.push_back(cur);
        if(cnt > 1) {
            string s = to_string(cnt);
            for(char c: s) {
                rets.push_back(c);
            }
        }

        chars = rets;

        return rets.size();
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *