jayati

String Compression

Mar 1st, 2023
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int compress(vector<char>& chars) {
  4.         int i=0,j=0;
  5.        
  6.         string s;
  7.         while(i<chars.size())
  8.         {
  9.            int count=1;
  10.            while(i+count<chars.size() && chars[i+count]==chars[i])
  11.            {
  12.                count++;
  13.            }
  14.            chars[j++] = chars[i];
  15.            if(count>1)
  16.            {
  17.                for(char c:to_string(count))
  18.                {
  19.                    chars[j++] = c;
  20.                }
  21.            }
  22.            i+=count;
  23.         }
  24.         return j;
  25.        
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment