Problem Statement
Several boxes are placed in a row. Each box is one of three colors. We want to rearrange these boxes in a such way that no three consecutive boxes are of the same color. This process should affect the fewest boxes possible. More formally, we want to achieve the desired configuration by swapping pairs of boxes, and we want to maximize the number of boxes that are never moved.
You will be given a String boxes which describes the colors of the boxes. Colors are given as characters 'A', 'B' and 'C' respectively. Rearrange the boxes as described above and return the maximum possible number of boxes that are never moved. Return -1 if it is not possible to achieve the desired configuration.
Definition
- Class:
- BoxesArrangement
- Method:
- maxUntouched
- Parameters:
- String
- Returns:
- int
- Method signature:
- int maxUntouched(String boxes)
- (be sure your method is public)
Constraints
- boxes will contain between 1 and 50 characters, inclusive.
- boxes will consist of characters 'A', 'B' and 'C' only.
Examples
"AAABBBCCC"
Returns: 6
The boxes could be rearranged into "ABABCBCAC"."AAAAAAAACBB"
Returns: 7
The best rearangement is "AABAABAACAA"."CCCCCB"
Returns: -1
"BAACAABAACAAA"
Returns: 5
"CBBABA"
Returns: 6