How do I make this algorithm more efficient? [code]
- User
How do I make this algorithm more efficient?
rustfn main() { let input = "3113322113".to_string(); let mut chars = input.chars().collect::<Vec<char>>(); for _ in 0..50 { let chars_len = chars.len(); let mut new_chars: Vec<char> = Vec::new(); let mut i: usize = 0; while i < chars_len { let mut j: usize = 1; let c = chars[i]; while i + j < chars_len { if c != chars[i + j] { break; } j += 1; } let mut j_to_chars = j.to_string().chars().collect::<Vec<char>>(); new_chars.append(&mut j_to_chars); new_chars.push(c); i += j; } chars = new_chars; dbg!(&chars.len()); } } - Assistant
To make the given algorithm more efficient, let's focus on a few specific areas: