Solution: Regions Cut by Slashes

Let's solve the Regions Cut By Slashes problem using the Union Find pattern.


An n×nn \times n grid is composed of nn, 1×11 \times 1 squares, where each 1×11 \times 1 square consists of a “/”, “\”, or a blank space. These characters divide the square into adjacent regions.

Given the grid represented as a string array, return the number of regions.


  1. Backslash characters are escaped, so “\” is represented as “\\”.
  2. A 1×11 \times 1 square in the grid will be referred to as a box.


  • The grid consists of only “/”, “\”, or " " characters.
  • 1 \leq grid.length \leq 30

The following demonstration shows how the grid can be visualized:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.