We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3776ab7 commit 59af794Copy full SHA for 59af794
443-string-compression/443-string-compression.cpp
@@ -0,0 +1,28 @@
1
+class Solution {
2
+public:
3
+ int compress(vector<char>& chars) {
4
+ int i = 0, ansIndex = 0;
5
+ int n = chars.size();
6
+ while(i<n){
7
+ int j = i+1;
8
+ while(j<n && chars[i] == chars[j]){
9
+ j++;
10
+ }
11
+
12
+ // ya toh vector pura traverse krr diyaa
13
+ // ya fir new/Different character encounter kia hai
14
15
+ chars[ansIndex++] = chars[i];
16
+ int count = j-i;
17
+ if(count > 1){
18
+ // converting counting into single digit and saving in answer
19
+ string cnt = to_string(count);
20
+ for(char ch: cnt){
21
+ chars[ansIndex++] = ch;
22
23
24
+ i = j;
25
26
+ return ansIndex;
27
28
+};
0 commit comments