Paint Bucket — Graph Extras

Repaint a tiny picture after a tap: the tapped pixel and every same-coloured pixel linked to it by sides take the chosen colour.

Graph Extras · Easy

The problem

An image is a grid of colour numbers. Starting at cell (sr, sc), repaint it with a new colour, and repaint every cell connected to it through up, down, left or right neighbours that have the same original colour as the starting cell. Return the repainted grid.

The story

A photo editor has a small picture of up to 5 by 5 pixels, each with a colour number from 0 to 3. The user taps one pixel with the paint bucket and picks a new colour. Every pixel that has the tapped pixel's colour and touches it by sides, directly or through other such pixels, takes the new colour. Return the picture after the tap.

What to return

Constraints

Example

Input
image = [[1, 1, 0], [1, 0, 0], [1, 0, 1]], sr = 1, sc = 1, color = 2
Output
[[1, 1, 2], [1, 2, 2], [1, 2, 1]]

Solve it

More in Graph Extras

All algorithm problems