Rust union-find
Intuition\nEach character is 4 segments: top, left, right, bottom. We can consider each segment as a vertex of the graph. Connecting them according the rules we
# Intuition\nEach character is 4 segments: top, left, right, bottom. We can consider each segment as a vertex of the graph. Connecting them according the rules we can calculate number of connected components that is the answer.\n**The rules:**\n\' \' => connect all 4 segments\n\'/\' => connect top and left, connect right and bottom.\n\'\\\\\' => connect top and right, connect left and bottom.\nThen connect each neighbor characters according to segments, for example, bottom segment of the first character in the first row is connected to the top segment of the first character in the second row. Right segment of the first character is connected to the left segment of the second character etc.\n\n# Code\n```\nuse std::collections::HashSet;\n\nimpl Solution {\n    pub fn regions_by_slashes(grid: Vec<String>) -> i32 {\n        let height = grid.len();\n        let width = grid[0].len();\n        let mut components = (0..width * height * 4).collect::<Vec<usize>>();\n        let mut result = (width * height * 4) as i32;\n        for (i, line) in grid.iter().enumerate() {\n            for (j, &ch) in line.as_bytes().iter().enumerate() {\n                match ch {\n                    b\' \' => {\n                        Self::union((i * width + j) * 4, (i * width + j) * 4 + 1, &mut components, &mut result);\n                        Self::union((i * width + j) * 4, (i * width + j) * 4 + 2, &mut components, &mut result);\n                        Self::union((i * width + j) * 4, (i * width + j) * 4 + 3, &mut components, &mut result);\n                    }\n                    b\'/\' => {\n                        Self::union((i * width + j) * 4, (i * width + j) * 4 + 1, &mut components, &mut result);\n                        Self::union((i * width + j) * 4 + 2, (i * width + j) * 4 + 3, &mut components, &mut result);\n                    }\n                    b\'\\\\\' => {\n                        Self::union((i * width + j) * 4, (i * width + j) * 4 + 2, &mut components, &mut result);\n                        Self::union((i * width + j) * 4 + 1, (i * width + j) * 4 + 3, &mut components, &mut result);\n                    }\n                    _ => panic!(""),\n                }\n                Self::connect(i, j, width, height, &mut components, &mut result);\n            }\n        }\n        result\n    }\n\n    fn union(n1: usize, n2: usize, components: &mut Vec<usize>, result: &mut i32) {\n        let p1 = Solution::find(n1, components);\n        let p2 = Solution::find(n2, components);\n        if p1 != p2 {\n            components[p2] = p1;\n            *result -= 1;\n        }\n    }\n\n    fn find(n: usize, components: &mut Vec<usize>) -> usize {\n        let mut p = components[n];\n        if p != n {\n            p = Solution::find(p, components);\n            components[n] = p;\n        }\n\n        return p;\n    }\n\n    fn connect(i: usize, j: usize, width: usize, height: usize, components: &mut Vec<usize>, result: &mut i32) {\n        if i > 0 {\n            Self::union((i * width + j) * 4, ((i - 1) * width + j) * 4 + 3, components, result);\n        }\n        if j > 0 {\n            Self::union((i * width + j) * 4 + 1, (i * width + j - 1) * 4 + 2, components, result);\n        }\n        if i < height - 1 {\n            Self::union((i * width + j) * 4 + 3, ((i + 1) * width + j) * 4, components, result);\n        }\n        if j < width - 1 {\n            Self::union((i * width + j) * 4 + 2, (i * width + j + 1) * 4 + 1, components, result);\n        }\n    }\n}\n```
['Rust']