class Solution:
    def compress(self, chars: List[str]) -> int:
        stri = ''
        stack = [chars.pop(0)]
        
        while chars:
            p = chars.pop(0)
            
            if p in stack:
                stack.append(p)
            else:
                stri = stri + stack[-1] + str(len(stack) if len(stack) > 1 else '')
                stack = [p] 
                
        o = list(stri + stack[-1] + str(len(stack) if len(stack) > 1 else ''))
        
        for i in o:
            chars.append(i)
class Solution {
    public int compress(char[] chars) {
        int index = 0;
        int i = 0;

        while (i < chars.length) {
            int j = i;

            while (j < chars.length && chars[j] == chars[i]) {
                j++;
            }

            chars[index++] = chars[i];

            if (j - i > 1) {
                String count = j - i + "";

                for (char c : count.toCharArray()) {
                    chars[index++] = c;
                }
            }

            i = j;
        }

        return index;
    }
}

// TC: O(n), SC: O(1)
String Compression
Given an array of characters chars, compress it using the following algorithm:

Begin with an empty string s. For each group of consecutive repeating characters in chars:


	If the group's length is 1, append the character to s.
	Otherwise, append the character followed by the group's length.


The compressed string s should not be returned separately, but instead, be stored in the input character array chars. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

After you are done modifying the input array, return the new length of the array.

You must write an algorithm that uses only constant extra space.

&nbsp;
Example 1:

Input: chars = ["a","a","b","b","c","c","c"]
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".


Example 2:

Input: chars = ["a"]
Output: Return 1, and the first character of the input array should be: ["a"]
Explanation: The only group is "a", which remains uncompressed since it's a single character.


Example 3:

Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".

&nbsp;
Constraints:


	1 &lt;= chars.length &lt;= 2000
	chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.


var compress = function(chars) {
    for(let i = 0; i < chars.length; i++){
        let count = 1
        while(chars[i] === chars[i+count]){
            delete chars[i+count]
            count++
        }
        if(count > 1){
            let count2 = 1
            String(count).split('').forEach((a) =>{
                chars[i+count2] = a
                count2++
            })
        }
        i = i + count -1
    }
    return chars.flat().filter((x) => x).length
};
class Solution {
public:

void add1(vector<int>& arr) {
    
    if(arr.back() < 9) {
        arr.back()++;
        return ;
    }
    
    reverse(begin(arr),end(arr));
    int carry = 1;
    
    for(int i=0;i<arr.size();i++) {
        
        if(arr[i] < 9) {arr[i]++;carry=0;break;}
        
        arr[i] = 0;
        carry = 1;
    }
    
    if(carry == 1) arr.push_back(1);
    reverse(begin(arr),end(arr));
}

int compress(vector<char>& chars) {
    
    int i=0;
    for(int j=0;j<chars.size();j++) {
        
        if(j == chars.size()-1 or chars[j] != chars[j+1]) {
            chars[i++] = chars[j];
        }
        
        else {
            
            vector<int> cnt{0};
            char ch = chars[j];
            while(j < chars.size()and chars[j] == ch) {
                j++;
                add1(cnt);
            }
            
            j--; // bcoz j will be incremented in for loop updation condition.
            chars[i++] = ch;
            
            for(auto& it:cnt)
                chars[i++] = '0'+it;
            
        }
    }
    chars.erase(chars.begin()+i,chars.end());
    return i;
}