diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /string_compression.rs | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'string_compression.rs')
-rw-r--r-- | string_compression.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/string_compression.rs b/string_compression.rs new file mode 100644 index 0000000..1d5db7d --- /dev/null +++ b/string_compression.rs @@ -0,0 +1,38 @@ +struct Solution; + +impl Solution { + pub fn compress(chars: &mut Vec<char>) -> i32 { + let (mut i, mut j, mut count, mut last_char) = (0, 1, 1, chars[0]); + while j < chars.len() { + if chars[j] == last_char { + count += 1; + } else { + chars[i] = last_char; + i += 1; + if count > 1 { + count.to_string().chars().for_each(|x| { + chars[i] = x; + i += 1; + }); + } + last_char = chars[j]; + count = 1; + } + j += 1; + } + chars[i] = last_char; + i += 1; + if count > 1 { + count.to_string().chars().for_each(|x| { + chars[i] = x; + i += 1; + }); + } + i as i32 + } +} + +fn main() { + let mut c = vec!['a','a','b','b','c','c','c']; + print!("{}", Solution::compress(&mut c)); +} |