TopCoder problem "SwappingMarbles" used in TCHS SRM 28 (Division I Level One)

Problem Statement


You have two rows of marbles. Each marble has one of three colors: red ('R'), green ('G') or blue ('B'), and no two adjacent marbles have the same color. Rows are not circular, so the first marble in a row is not adjacent to the last.

You are given the Strings colors1 and colors2 representing the colors of the marbles from left to right of each row, respectively. You are allowed to perform exactly one swap operation, where you select one marble from each row and physically swap them. Return the number of different swap operations that result in each row containing three adjacent marbles with the same color. Note that the three adjacent marbles within each row must have the same color, but that color does not necessarily have to be the same between the two rows (see examples).



Parameters:String, String
Method signature:int swaptions(String colors1, String colors2)
(be sure your method is public)


-colors1 and colors2 will each have between 3 and 50 characters, inclusive.
-Each character in colors1 and colors2 will be one of the following: 'R', 'G' or 'B'.
-Any two adjacent characters in colors1 will be different.
-Any two adjacent characters in colors2 will be different.


Returns: 1
The only acceptable way is to swap the 5-th marble from the first row ('B') with the second marble from the second ('R'). After this operation the rows will be equal to "RGBRRR" and "BBBGRG", respectively.
Returns: 2
Here you can swap the second marble from the second row ('R') with either the second or the fourth marble from the first row (both are 'G').
Returns: 0
Returns: 1152
Returns: 41

Problem url:

Problem stats url:




PabloGilberto , brett1479 , Olexiy

Problem categories:

Brute Force, Simple Search, Iteration