Skip to content

Instantly share code, notes, and snippets.

@misha-krainik
Created October 19, 2022 18:29
Show Gist options
  • Save misha-krainik/f01c64d5ecc370aed31edb5614dfc8f3 to your computer and use it in GitHub Desktop.
Save misha-krainik/f01c64d5ecc370aed31edb5614dfc8f3 to your computer and use it in GitHub Desktop.
Remove the parentheses
use std::collections::LinkedList;
use std::iter::FromIterator;
fn remove_parentheses(s: &str) -> String {
let w: LinkedList<char> = s.chars().collect();
let mut skip_count = 0;
String::from_iter(w.into_iter().filter_map(|l| {
if l == '(' { skip_count += 1 }
let r = if skip_count != 0 {
None
} else {
Some(l)
};
if l == ')' { skip_count -= 1 }
r
}).collect::<Vec<char>>())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment