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).

 

Definition

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

Constraints

-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.
 

Examples

0)
    
"RGBRBR"
"BRBGRG"
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.
1)
    
"RGRGR"
"GRG"
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').
2)
    
"BGBGBGBG"
"RBRBRBRBRB"
Returns: 0
3)
    
"RBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB"
"BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBR"
Returns: 1152
4)
    
"BRGBRGBRBRBGRBRBRBGRGBRGBRGBRGBRGB"
"RBGBGBRBRBGRGBRBRBGRBGRBGBGBRBGBRBGRGRGBRBRBR"
Returns: 41

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6882

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10652&pm=6882

Writer:

supernova

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Brute Force, Simple Search, Iteration