Skip to content

Commit 4368c41

Browse files
author
kaidul
committed
Facebook phone interview passed but hiring process freezed anyway. Bloomberg phone interview ahead
1 parent 791754e commit 4368c41

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

source-code/String_Compression.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int compress(vector<char>& chars) {
4+
int n = (int)chars.size();
5+
if(chars.empty()) return 0;
6+
int left = 0, right = 0, currCharIndx = left;
7+
while(right < n) {
8+
if(chars[currCharIndx] != chars[right]) {
9+
int len = right - currCharIndx;
10+
chars[left++] = chars[currCharIndx];
11+
if(len > 1) {
12+
string freq = to_string(len);
13+
for(int i = 0; i < (int)freq.length(); i++) {
14+
chars[left++] = freq[i];
15+
}
16+
}
17+
currCharIndx = right;
18+
}
19+
right++;
20+
}
21+
22+
int len = right - currCharIndx;
23+
chars[left++] = chars[currCharIndx];
24+
if(len > 1) {
25+
string freq = to_string(len);
26+
for(int i = 0; i < freq.length(); i++) {
27+
chars[left++] = freq[i];
28+
}
29+
}
30+
return left;
31+
}
32+
};

0 commit comments

Comments
 (0)