Skip to the code
cf.
+25 XP

#424 Fill Enclosed Gaps

medium

A map editor cleans up drawings: any empty area completely closed in by walls should be filled in. The map is a grid of strings where "X" is a wall and "O" is empty. Empty cells that touch up, down, left, or right form one area.

An area is enclosed if none of its cells is on the edge of the map. Write fillGaps(map) that returns a new map, as an array of strings, where every cell of every enclosed area becomes "X". Areas that touch the edge stay as they are.

Example 1

Input: map = ["XXXX","XOOX","XXOX","XOXX"]
Output: ["XXXX","XXXX","XXXX","XOXX"]

The three Os in the middle can't reach the edge, so they're filled. The O on the bottom row touches the edge and stays.

Example 2

Input: map = ["OO","OO"]
Output: ["OO","OO"]

Every empty cell is on the edge.

Constraints

  • 1 <= map.length, map[i].length <= 200
  • Each cell is "X" or "O".
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).