LeetCode/rs/path-crossing.rs
Matej Focko 835b0208b9
rs: add “1496. Path Crossing”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-12-23 13:42:48 +01:00

27 lines
603 B
Rust

use std::collections::HashSet;
impl Solution {
pub fn is_path_crossing(path: String) -> bool {
let mut visited = HashSet::from([(0, 0)]);
let mut x = 0;
let mut y = 0;
for d in path.chars() {
match d {
'N' => y -= 1,
'S' => y += 1,
'E' => x += 1,
'W' => x -= 1,
_ => unreachable!("invalid direction"),
}
if visited.contains(&(x, y)) {
return true;
}
visited.insert((x, y));
}
false
}
}