Coloring A Border is a fascinating grid traversal problem that combines graph theory with practical visualization concepts.

You're given an m x n integer matrix grid where each cell represents a colored square, and three integers: row, col, and color. Your task is to identify a connected component (a group of adjacent cells with the same color) starting from grid[row][col], find its border, and color that border with the new color.

What is a border? A cell in the connected component is part of the border if:
• It's on the edge of the grid (first/last row/column), OR
• It's adjacent to at least one cell that's NOT part of the same connected component

Goal: Return the modified grid after coloring the border of the connected component containing grid[row][col].

Input & Output

example_1.py — Basic Connected Component
$ Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output: [[3,3],[3,2]]
💡 Note: The connected component starting at (0,0) consists of cells with value 1: (0,0), (0,1), and (1,0). All these cells are border cells because they're either on the grid boundary or adjacent to cell (1,1) which has a different color. So all three cells get colored with 3.
example_2.py — Interior vs Border Cells
$ Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
Output: [[2,2,2],[2,1,2],[2,2,2]]
💡 Note: The entire grid forms one connected component of 1's. The center cell (1,1) is interior (surrounded by same-colored cells), while all edge cells are border cells. Only the border gets colored with 2, leaving the center cell unchanged.
example_3.py — Same Color Edge Case
$ Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 1
Output: [[1,1],[1,2]]
💡 Note: Since the target color (1) is the same as the original color of the connected component, no changes are made to the grid.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 50
  • 1 ≤ grid[i][j], color ≤ 1000
  • 0 ≤ row < m
  • 0 ≤ col < n

Visualization

Tap to expand
Coloring A Border - DFS Approach INPUT Original Grid (2x2) 1 1 1 2 (0,0) (0,1) 0 1 Start: (0,0) Parameters grid = [[1,1],[1,2]] row = 0 col = 0 color = 3 ALGORITHM STEPS 1 Start DFS Begin at (0,0), color=1 2 Find Component Explore connected cells with same color (1) 2 Connected component 3 Identify Border Check if cell is on edge or adjacent to diff color 4 Color Border Apply new color (3) to all border cells Border cells FINAL RESULT Modified Grid 3 3 3 2 Border Unchanged Output [[3,3],[3,2]] OK - Complete All 3 cells with value 1 are on grid edge or adjacent to value 2 Key Insight: A border cell must satisfy: (1) It belongs to the connected component starting from (row, col), AND (2) It's either on the grid boundary OR has at least one neighbor outside the component. DFS explores all connected cells while tracking which ones qualify as border cells for recoloring. TutorialsPoint - Coloring A Border | DFS Approach
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen