In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. These characters divide the square into contiguous regions.
(Note that backslash characters are escaped, so a \ is represented as “\\“.)
Return the number of regions.
Example
No.1
Input:
1 | [ |
Output: 2
Explanation: The 2x2 grid is as follows:
No.2
Input:
1 | [ |
Output: 1
Explanation: The 2x2 grid is as follows:
No.3
Input:
1 | [ |
Output: 4
Explanation: (Recall that because \ characters are escaped, “\\/“ refers to \/, and “/\\“ refers to /\.)
The 2x2 grid is as follows:
No.4
Input:
1 | [ |
Output: 5
Explanation: (Recall that because \ characters are escaped, “/\\“ refers to /\, and “\\/“ refers to \/.)
The 2x2 grid is as follows:
No.5
Input:
1 | [ |
Output: 3
Explanation: The 2x2 grid is as follows:
Note
- 1 <= grid.length == grid[0].length <= 30
- grid[i][j] is either ‘/‘, ‘\‘, or ‘ ‘.
Code
1 | public class UnionFind { |