#959

Regions Cut By Slashes

pupil · 485 · lc medium +27 · verified · 77.5% accepted · 3,984 likes · top 89%

Description

You have an n x n grid where each cell holds '/', '\', or ' '. These characters cut unit cells diagonally into sub-regions. Count and return the total number of distinct contiguous regions across the entire grid.

Example 1:

Input: grid = [" /","/ "]
Output: 2

Example 2:

Input: grid = [" /"," "]
Output: 1

Example 3:

Input: grid = ["/\\","\\/"]
Output: 5
Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.

Code

1
2
3