Statistics

Problem Statement for "SwappingMarbles"

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

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

  2. "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').

  3. "BGBGBGBG"

    "RBRBRBRBRB"

    Returns: 0

  4. "RBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB"

    "BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBR"

    Returns: 1152

  5. "BRGBRGBRBRBGRBRBRBGRGBRGBRGBRGBRGB"

    "RBGBGBRBRBGRGBRBRBGRBGRBGBGBRBGBRBGRGRGBRBRBR"

    Returns: 41

  6. "RGBG"

    "BGBGBGB"

    Returns: 3

  7. "BGB"

    "BGB"

    Returns: 0

  8. "BRB"

    "RBR"

    Returns: 1

  9. "RBR"

    "GRBRGBGRBGBGRGRGRBGRGBGBGBGBGRGRGRGRGRBRGBRGRGRBGR"

    Returns: 0

  10. "BRGBRBGBRBG"

    "RGRGRGRBGRGRGRGRGBGBGBRGRGRGRGRGRGB"

    Returns: 2

  11. "GRBRGBRGRGBRGRGBRBGRGRBRGRGRGRGBGRBGBRGRGR"

    "BGRGBRGRBGRBRGRGBGRBGRBGRBGBGRBGRGRGBR"

    Returns: 57

  12. "GRGRGRBRBRGRGRGRGRGBRGRBRBRBRBGRGRGBRBGRBRGRBRBGRB"

    "RGRBGRBGBGRGBRGRGBGBRGBGRGRGRGBGRGRGRGBGRGBGBGBGRG"

    Returns: 138

  13. "BRBRGBRGBG"

    "BGR"

    Returns: 0

  14. "GRGRGBGRBGB"

    "BGRGRGRBGRGBGRGBGRGRGRGRBGBGBGBGBRGR"

    Returns: 28

  15. "BRGRBRGBGBRBRGR"

    "BGRGBGRBGBGRGRBGRGRBRGRGRGBGRBGRGRBGBGRBRGRGRGR"

    Returns: 24

  16. "GBRGRGRGRBGRBRGRB"

    "GRGRGBGRGBGBGRGRBGRGRGRGRGRBGRGRBGRGRGBRGBGRGRBRBR"

    Returns: 67

  17. "GRB"

    "BRGRGRGBGBGRGBGRGRBGRBRGRGRGRGRGRBGRGRGRGRGRGRBRGR"

    Returns: 0

  18. "GRGRBGR"

    "BRBRGRGRBG"

    Returns: 3

  19. "RBRBRBGRGRGRBRGRGRGRGBRBRB"

    "BRGBGRGRGRGRGRGRGRGBGRGRGRGR"

    Returns: 95

  20. "BRGBRGRBRBRGBRBRGRBRBRGRBRGRGRGRGRBRBRGRGBRGRGRGRG"

    "RBGBGRBGBGRGBRGRBGBGBGRGRGBGBGRGRBRBRGRBRGBRBGRBRB"

    Returns: 112

  21. "BGBRGRBRBRGRGBGRGRBRGRBGRGRGRBGBGRGBRBRGRBRGRGBRGR"

    "BGRGBG"

    Returns: 11

  22. "BRGRGRGBRBRGRGRGBGRGRGRGBRGRBGRGRGRBRGRGRB"

    "RGRBGRBRGRGBGRGRGBGBGRGRBGRGRBGBGRGRBGRGRGR"

    Returns: 171

  23. "GRGBGBRGBRGRGRBRGRGBRBRBRGRGBRBRBRGBGRGBRGBGRBRBRB"

    "GRBGRGBGRGRBRGBRBGBGRGBRGRGBGRGBGRBRGRBGRGRBRGRGRG"

    Returns: 94

  24. "BRGBRBRBGRGRBRBRGRBGRGRGRGB"

    "GRGRGRGRBRGBGRBGBGRGBGBGBGRGBGRGRBRGBGRGRGRGBGRBRG"

    Returns: 69

  25. "GRGRGRGRBGRGRGRGBGB"

    "RGR"

    Returns: 6

  26. "RBGBGRGBGRGRBRBRGBRBGBRGRBRGRGRGRBRBGRGBRGRBGRGBGB"

    "GRBGRGRGRGBGRGRBGRGRGRBRGRGRBRG"

    Returns: 93

  27. "RBGRBRGRGRGRGRBRBRGRGRGRGBGR"

    "BGRBRGRGRGRGRGRBRGRGBGRBGBRBGRBGRBGRGRGBRGBRBRGRBG"

    Returns: 107

  28. "RBGRGRBRGRBRGRGBGRGRGRBRGBRGRBRGRGRGRGBGBRBRBRBRGR"

    "GRBRGRBGBRBGBGRGB"

    Returns: 32

  29. "RGBRGRGRBRGRGRBGRBGRGRG"

    "BRBRGRGRGBRGRGBGBGBGBGRGRGRGRGBGRBGRGRGBGBGBGRGBRB"

    Returns: 80

  30. "RGBRGRBRBRGRBGR"

    "RBG"

    Returns: 0

  31. "GBGRBRGRGBRGRGRBRGBRGBRGBRBGBRGRB"

    "BGRGRGRGBGRBRGBGBGRGRGRBGBGBGBRBG"

    Returns: 40

  32. "GRGRBRBRGRGRBRBGBRBRGBGBGBGBRGBGRGBGRBGBGRBGBR"

    "RBGRBRGRBGRGBRBRBGRGRGRBGRBRGRGBGR"

    Returns: 47

  33. "RGBGRBRBGBGRGRBGBRGRBGRBRGBRGBRGRBRBRBGRBRGBRGRBRG"

    "RGRBGBGRGRGRGBGRBGRGRGRBGRGRBGRGRGBRGRBGBRG"

    Returns: 48

  34. "RGR"

    "RBGRGRBGRGRBRGRBRBRBGB"

    Returns: 2

  35. "GBRBRBRGRGBRGRGRGBGRGRBGBGRBRGBRGRGRGBGRGBGRGRBGRB"

    "RBGBRGBRBRBGRBRGBGRGBRGRGBRBGRB"

    Returns: 40

  36. "RGBGRBG"

    "RBGRGR"

    Returns: 0

  37. "BGRBRGBRBRBGBGRGRGRGRGBGBRGBRGRGRBRGRBRGRGBRBRBRBR"

    "GRGRGBGRBGRBGRBGBGBGRGRGRGRBGRBRBGBGBGB"

    Returns: 91

  38. "BRBRGRGBRGRGRGRGBRGRGRBRGRGBRGRBGBGBRGRBGRGRGRGRGR"

    "BGRBRGRBGRBGRGRBGBRGRGRGBRBRBRGRBGRBGBGBGRGRGRGRBR"

    Returns: 173

  39. "BGRGBGBRGRBGBRGRBRGRGBRGRGRGBGRGRGRGBRBGRGRGBRBGRG"

    "BGBRGRBGRGBGBGRBRGRGRGRBGBGRGRGRGRBRGRGRGRGRGBGRBG"

    Returns: 208

  40. "GBRGRBRGRGRBRGBRGBRGBR"

    "GRGRGRGRGRBGRGRGBGRGBGRGRGBGRGRGRGRGRGBGRGBGBGRBGB"

    Returns: 55

  41. "GRBGR"

    "RGBGRGRGRBRBGRBGBGBRGRGRGBGRGRGBGRGRGBGBG"

    Returns: 0

  42. "RBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRB"

    "BRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBRBR"

    Returns: 1152

  43. "BRB"

    "RBR"

    Returns: 1

  44. "RBRGBR"

    "BRBGBR"

    Returns: 1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: