Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
535 B
0
Indexable
Never
#define pb(i) push_back(i)
class Solution {
public:
    int compress(vector<char>& arr) {
        
        int n=arr.size();
        if(n==1) return arr.size();
        vector<char>res;
        for(int i=0;i<n;){
            int count=0;
            int j=i;
            while(j<n &&arr[j]==arr[i]) {
                count++;
                j++;
            }
            res.pb(arr[i]);
            if(count>1) for(auto i:to_string(count)) res.pb(i);
            i=j;
        }
        arr = res;
        return arr.size();
    }
};
Leave a Comment