-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_compression.rs
35 lines (29 loc) · 1.06 KB
/
string_compression.rs
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
impl Solution {
pub fn compress(chars: &mut Vec<char>) -> i32 {
let (mut i, n) = (0, chars.len());
let mut new_len = 0;
while i < n {
let mut j = i;
// increment j until we find a different character or reach the end
while j < n && chars[j] == chars[i] {
j += 1;
}
// place the character at the new position
// e.g. if we have aabbccc, we place a at the start of the array
chars[new_len] = chars[i];
new_len += 1;
// if the length of the group of characters is greater than 1
// i.e. suppose if new_len is 12, we need to place 12 as characters [..., '1','2', ...]
if j - i > 1 {
for c in (j - i).to_string().chars() {
chars[new_len] = c;
new_len += 1;
}
}
// place i at same position as j,
// i.e. the start of the next group of characters
i = j;
}
new_len as i32
}
}