Regions Cut By Slashes - Problem
An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.
Given the grid grid represented as a string array, return the number of regions.
Note that backslash characters are escaped, so a '\' is represented as '\\'.
Input & Output
Example 1 — Basic Slash Division
$
Input:
grid = [" /","/ "]
›
Output:
2
💡 Note:
The 2x2 grid has a '/' in positions (0,1) and (1,0). These slashes divide the grid into 2 separate regions: one triangle-shaped region in the top-left and bottom-right corners, and another in the top-right and bottom-left corners.
Example 2 — Backslash Division
$
Input:
grid = [" \\","\\ "]
›
Output:
2
💡 Note:
The backslashes create a different pattern than forward slashes, but still divide the 2x2 grid into 2 regions. The backslashes connect opposite corners, creating 2 triangle-shaped regions.
Example 3 — No Division
$
Input:
grid = [" "," "]
›
Output:
1
💡 Note:
All cells are empty spaces, so there are no barriers dividing the grid. The entire 2x2 area forms one single connected region.
Constraints
- n == grid.length == grid[i].length
- 1 ≤ n ≤ 30
- grid[i][j] is either '/', '\\', or ' '
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
2×2 grid with forward slashes: [" /", "/ "]
2
Slash Barriers
Slashes act as barriers dividing the space
3
Count Regions
Result: 2 separate triangular regions
Key Takeaway
🎯 Key Insight: Scale up the grid 3x to clearly visualize how slashes divide regions
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code