-
Notifications
You must be signed in to change notification settings - Fork 0
/
443. String Compression.cpp
49 lines (39 loc) · 1.09 KB
/
443. String Compression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
problem link :https://leetcode.com/problems/string-compression/
problem name: 443. String Compression
status: Accepted
author : Moahnd sakr
** */
class Solution {
public:
int compress(vector<char>& chars) {
string s="";
char currentChar=chars[0];
int frequancy=1;
int charsLength=chars.size();
int curindex=0;
for(int i=1;i<charsLength;i++){
if(currentChar==chars[i]){
++frequancy;
}
else {
s+=currentChar;
if(frequancy>1)
{
s+= to_string(frequancy);
}
currentChar=chars[i];
frequancy=1;
}
}
s+=currentChar;
if(frequancy>1){
s+= to_string(frequancy);
}
int answer= s.length();
for(int i=0;i<answer;i++){
chars[i]=s[i];
}
return answer ;
}
};