LeetCode 1056. Confusing Number

Problem Statement

Backtrack.


use std::collections::HashMap;

impl Solution {
pub fn confusing_number(n: i32) -> bool {
let s = n.to_string();

let map: HashMap<char, char> = [('1', '1'), ('6', '9'), ('8', '8'), ('9', '6'), ('0','0')]
.iter().cloned().collect();
let (mut changed, mut i, mut j) = (false, 0, s.len() - 1);
while i <= j {
if !map.contains_key(&s.chars().nth(i).unwrap())
|| !map.contains_key(&s.chars().nth(j).unwrap()) {
return false;
}
if map[&s.chars().nth(i).unwrap()] != s.chars().nth(j).unwrap() {
changed = true;
}
i += 1;
if j != 0 {
j -= 1;
}
}

return changed;
}
}