Advertisement
jayati

String Compression

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