I have a string containing only two char values: 'a'
or 'b'
. A char can be swapped in for the other char value. Each char in the string has an associated cost with swapping it. I need to find the minimum cost of swaps such that there are no 3 consecutive chars with the same value in the resulting string.
If we have a block of consecutive chars with length 3, then we just swap the minimum cost char. If we have a block greater than length 3, then we have a number of possibilities for swaps. I don't know how to handle this case. How can I decide which chars to swap in a block greater than length 3 efficiently?