You are given a 2D grid of size m x n, where each cell contains a ball painted with a color, represented by a lowercase English letter ('a' to 'z'). Your goal is to recolor the balls such that:
You may recolor any ball using any lowercase letter from 'a' to 'z'
Return the minimum number of recolorings required to make the grid valid according to the constraints.
Example
Input:
grid = [
['a', 'a', 'a'],
['b', 'b', 'b']
]
Output:
2
Explanation:
In row 0: 'a' appears 3 times, which violates the "at most 2 per row" rule.
Recolor one 'a' to another color (say 'c'), and ensure no two adjacent cells have the same color.
Row 1 also needs 1 change.
So, minimum 2 changes required.
Constraints