Ctrl K

A Board Cut by Slashes

Medium
A square n × n board is made of cells. Each cell is either empty (a space ' ') or crossed by a diagonal: '/' runs from the lower-left corner to the upper-right one, '\' from the upper-left corner to the lower-right one. The board is given as a list of strings tiles, where tiles[i][j] is the content of a cell. The diagonals act as walls. Return how many connected regions they split the board into. Note that a backslash is escaped in code, so the string "\\/" stands for the two cells \ and /.
Example 1
tiles=["/\\", "  "] → 3

The / and \ in the top row form a roof: two top corners are cut off, and everything under the roof is the third region.

Example 2
tiles=["\\/", "/\\"] → 4

The strokes form one big X across the whole board and split it into 4 triangles.

Example 3
tiles=[" / ", "/ /", " / "] → 3
Constraints
  • 1 ≤ n == len(tiles) == len(tiles[i]) ≤ 30
  • tiles[i][j] is '/', '\' or ' '
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTotal of the Unreachable MediumDrain the Landlocked Ponds MediumLand Regions on a Map MediumLargest Forest Patch MediumMerge Contact Cards Medium
loading Python…
Input
tiles=["/\\", "  "]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 23 tests on the server, hidden ones included.