400. Warehouse Repair Areas
Warehouse Repair Areas

A warehouse floor is divided into rows and columns of square tiles. Each tile is either damaged or usable.

Damaged tiles that share a horizontal or vertical side belong to the same repair area. Tiles that touch only at a corner are not connected.

Find the number of separate repair areas on the warehouse floor.

Method Signature

int countRepairAreas(List<String> floorPlan)

Parameters

  • floorPlan: The condition of the warehouse floor.
  • Each string represents one row of tiles.
  • 'X' represents a damaged tile.
  • '.' represents a usable tile.

Returns

The number of separate repair areas.

Connection Rules

  • Damaged tiles are connected only when they share a side.
  • Diagonally adjacent damaged tiles belong to different repair areas unless another path connects them.
  • Every connected group of damaged tiles forms one repair area.
  • The space outside the warehouse floor is considered usable.

Constraints

  • 1 ≤ floorPlan.size() ≤ 300
  • 1 ≤ floorPlan.get(i).length() ≤ 300
  • Every string in floorPlan has the same length.
  • Every character in floorPlan is either 'X' or '.'.

Examples

Example 1

countRepairAreas( floorPlan = List.of(".XX..", ".X...", ".XXX.", "....."))

Returns 1.

All damaged tiles are connected through shared sides, so they form one repair area.

Example 2

countRepairAreas( floorPlan = List.of("X...X", "XX..X", ".....", "..XX.", "...X."))

Returns 3.

The damaged tiles form separate groups in the upper-left, upper-right, and lower parts of the floor.

Example 3

countRepairAreas( floorPlan = List.of("X.X", "...", "X.X"))

Returns 4.

None of the four damaged tiles shares a side with another damaged tile.

Example 4

countRepairAreas( floorPlan = List.of("....", "...."))

Returns 0.

The floor contains no damaged tiles.



Please use Laptop/Desktop or any other large screen to add/edit code.