optimizations

master
attila 4 years ago
parent 93eb6067f3
commit c9c3c17302

@ -24,42 +24,22 @@ fn main() {
}
fn reduce(string: String) -> i32 {
let mut current_string = string;
let mut new_string: Vec<char>;
let mut last_string = String::new();
let all_chars: Vec<char> = string.chars().into_iter().collect();
while last_string != current_string {
let all_chars: Vec<char> = current_string.chars().into_iter().collect();
let mut count = 0;
let mut stack: Vec<char> = Vec::new();
new_string = Vec::new();
while count < all_chars.len() - 1 {
let cur_char_uppercase = all_chars[count].to_uppercase().collect::<Vec<char>>()[0];
let cur_char_lowercase = all_chars[count].to_lowercase().collect::<Vec<char>>()[0];
if all_chars[count] == cur_char_lowercase {
if all_chars[count + 1] == cur_char_uppercase{
count += 1;
} else {
new_string.push(all_chars[count].clone());
}
} else {
if all_chars[count + 1] == cur_char_lowercase{
count += 1;
} else {
new_string.push(all_chars[count].clone());
}
for c in all_chars{
if !stack.is_empty(){
let sub = *stack.last().unwrap() as i32 - c as i32;
if sub == 32 || sub == -32 {
stack.pop();
} else{
stack.push(c.clone());
}
count += 1;
} else {
stack.push(c.clone());
}
new_string.push(all_chars[all_chars.len() - 1].clone());
last_string= current_string.clone();
current_string = new_string.clone().into_iter().collect();
}
return (current_string.len()-1) as i32
}
return stack.len() as i32
}
Loading…
Cancel
Save