Repaint a Region
A picture is given as a list of equal-length strings, picture, one per row; each character is the colour of one cell. Starting at the cell in row row and column column, repaint it with the colour colour, together with every cell of the same colour connected to it: cells of that colour that you can reach from the start by stepping up, down, left or right through cells of that colour.
Return the picture after repainting, in the same form. Cells that only touch the region diagonally keep their colour.
Examples
picture = ["aab", "abb", "ccb"], row = 0, column = 0, colour = "z"
→ ["zzb", "zbb", "ccb"]
picture = ["aab", "abb", "ccb"], row = 1, column = 2, colour = "a"
→ ["aaa", "aaa", "cca"]
picture = ["xyx", "yxy"], row = 0, column = 1, colour = "y"
→ ["xyx", "yxy"]
picture = ["...", ".#.", "..."], row = 1, column = 1, colour = "o"
→ ["...", ".o.", "..."]In the first example the three as are joined through the corner cell, so all three are repainted; the bs and cs keep their colour. In the second, the four bs are joined and become as. In the third, the cell already has the colour y, so nothing changes. In the last, the # has no neighbour of its own colour, so it is repainted alone.
Constraints
- Up to 100 rows and 100 columns;
rowandcolumnare inside the picture. colouris a single character.- Aim for O(n) in the number of cells: reach each cell once.
Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.