15% off all plans & roadmaps Use code
See plansLeetCode 959 · Solution & editorial
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.
Regions Cut By Slashes is a Graph Traversal Patterns (DFS & BFS) problem, in the Union-Find (Disjoint Set Union - DSU) subpattern. The pattern covers DFS/BFS traversal, topological sort, shortest paths, Union-Find, and MST
Master Union-Find data structure with path compression and union by rank for connectivity problems.
The reference solution runs in O(N^2) time and O(N^2) space.
Regions Cut By Slashes has been reported in interviews at Uber.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Regions Cut By Slashes is a Graph Traversal Patterns (DFS & BFS) problem, in the Union-Find (Disjoint Set Union - DSU) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.