DEV Community

Discussion on: Check if the chain has the same amount

Collapse
 
jay profile image
Jay

Rust Solution.

fn check_x_o(s: &str) -> bool {
  s.to_lowercase()
    .chars()
    .map(|c| match c {
      'x' => 1,
      'o' => -1,
      _ => 0,
    })
    .sum::<i32>() == 0
}

// Usage
fn main() {
  let st = "xoxABCxo";
  println!("{}", check_x_o(&st));    // false
}