Advertisement
Guest User

Bracket Simplification

a guest
Aug 22nd, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.44 KB | Source Code | 0 0
  1. fn main() {
  2.     let input = std::env::args().skip(1).next().unwrap();
  3.     let mut stack = Vec::with_capacity(input.len() / 4);
  4.     for ch in input.chars() {
  5.         match (ch, stack.last()) {
  6.             ('}', Some(&'{')) | (']', Some(&'[')) | (')', Some(&'(')) => {
  7.                 stack.pop();
  8.             }
  9.             _ => stack.push(ch),
  10.         }
  11.     }
  12.     let output: String = stack.iter().collect();
  13.     println!("{}", output);
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement