Statistics

Problem Statement for "LittleElephantAndBalls"

Problem Statement

Little Elephant from the Zoo of Lviv likes balls. He has some balls that he wants to arrange into a row on the table. Each of those balls has one of three possible colors: red, green, or blue.

You are given a String S. This string represents all of the balls Little Elephant has, in the order in which he will be placing them onto the table. Red, green, and blue balls are represented by the characters 'R', 'G', and 'B', respectively. Each time Little Elephant places a new ball onto the table, he may add it anywhere into the row of already placed balls.

Additionally, each time Little Elephant adds a ball to the table, he scores some points (possibly zero). The number of points is calculated as follows:

  • If this is the first ball being placed on the table, there are 0 points for it.
  • If he adds the current ball to one of the ends of the row, the number of points scored is equal to the number of different colors of the balls on the table, excluding the current ball.
  • If he adds the current ball between two other balls, the number of points scored is equal to the number of different colors of the balls before the current ball, plus the number of different colors of the balls after the current ball.

For example, suppose that the balls currently on the table form the row "GBBG". Little Elephant now wants to add a new red ball ('R'). One of the options is to add it to the beginning. This scores 2 points and produces the row "RGBBG". Another option is to add it between "GBB" and "G". There are 2 distinct colors in "GBB" and 1 in "G", so this move is worth 2+1 = 3 points. This move produces the row "GBBRG".

Return the maximum total number of points that Little Elephant can earn for placing the balls onto the table.

Definition

Class:
LittleElephantAndBalls
Method:
getNumber
Parameters:
String
Returns:
int
Method signature:
int getNumber(String S)
(be sure your method is public)

Constraints

  • S will contain between 1 and 50 characters, inclusive.
  • S will consist only of characters 'R', 'G' and 'B'.

Examples

  1. "RGB"

    Returns: 3

    Any strategy is optimal here. Each strategy scores 0+1+2 = 3 points.

  2. "RGGRBBB"

    Returns: 21

  3. "RRRGBRR"

    Returns: 16

  4. "RRRR"

    Returns: 5

  5. "GGRRRGR"

    Returns: 18

  6. "G"

    Returns: 0

  7. "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"

    Returns: 97

  8. "RRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG"

    Returns: 144

  9. "RRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGBBBBBBBBBBBBBBBBBB"

    Returns: 195

  10. "RRGGBBRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"

    Returns: 279

  11. "RGRBGRBRGGRBRGRGGBRGRB"

    Returns: 110

  12. "G"

    Returns: 0

  13. "B"

    Returns: 0

  14. "RGGGRGGRGRGGRGRGRGBBB"

    Returns: 76

  15. "RGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRG"

    Returns: 279

  16. "RGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRGRG"

    Returns: 190

  17. "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRBG"

    Returns: 98

  18. "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"

    Returns: 97

  19. "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"

    Returns: 97

  20. "RRRRRRRRRGRRRRRRGGRGRGGRGBGRRRGRGGRGRBBBRGRGRGGRGR"

    Returns: 206

  21. "RRRRRRRRRRRGRRRRRRRRRRRRRRRRRRRRBRRRRRRRRRRRRRRRGB"

    Returns: 153

  22. "BBGGBGRRGBBBGRGBGBRRRRBRGGRBBBBRBRRGBGGRBRBRRBRGBG"

    Returns: 275

  23. "BBGRRGBRGGGBBRRGGBGGBRBRGBRRRBRBRGRBGBBGRRBGBGBRRB"

    Returns: 279

  24. "RBGRRRRBBRBBBGRRBRRRGRBRBBRGBGBGRGGGRBGRGBRRBBGBBR"

    Returns: 268

  25. "BGBGRBRBRBGBRBBRGRBBGBBGRRRRRGGRBGRBRBGBBGRGBRRBGG"

    Returns: 278

  26. "BBBRBBGBGRBGRBGBRGRRGRGBBRRRRBBBBBGGRRGGBBGBBBRGGR"

    Returns: 267

  27. "BRBRRBGBBBGRRRBRBRGBBRGBBBBBRGBRRRRGGGBGGGRRGBBRBB"

    Returns: 272

  28. "BRGRBGBGGBGRGRBRGGGGGGRGGBRGBGBGBRRGBGBBBGRGRBBBBR"

    Returns: 279

  29. "GGRGGGRBGGRBGBBBRRRBRBBRGRGBBBGBRBBGBGBRRBRBGBBGRB"

    Returns: 267

  30. "GBBGBBRBBGBGBRGGGBBRGRRBRBBGBBBGBBRBBGGBGBBBGGBBGR"

    Returns: 269

  31. "BGBGBGGBGRGBGRBGGRGRGBGBRBGRRRGGRGRBRGGBGBBGRBB"

    Returns: 248

  32. "GGBGGGGBGGRRBRGRBBGBGBBRGGGGBBRR"

    Returns: 155

  33. "GBGGBRBBGGBRRRRGRGRGGRGGGRBRRRGRRGRBBGBRRBBRRGBGG"

    Returns: 265

  34. "RBBRBGRGGBBBRBBGBRGBRBRGGRRBBBRBGGRGGRGBBBRGRGBR"

    Returns: 264

  35. "BRGRGRBBBGRBRBR"

    Returns: 68

  36. "GBGBBBGRGBBBBRG"

    Returns: 58

  37. "BGRRGGRRRGRBBBB"

    Returns: 63

  38. "RGGRGRGRRBRGGBRBGRBRB"

    Returns: 92

  39. "BGBBBBBBBBBBBBBBBBBBBBBBBBBBRGBGBGBGBRRGBGBRGBGBRR"

    Returns: 197

  40. "GGGBGGGRRRRRRRRRBGGGGGGGGGRRRRRRRRRRBBBBGGGRRBGRGR"

    Returns: 259

  41. "GGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"

    Returns: 154

  42. "RBRBGGGGG"

    Returns: 33

  43. "RRRGGGBBB"

    Returns: 27

  44. "RGBRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"

    Returns: 142

  45. "RBBBBRGGGGGRRRGB"

    Returns: 69

  46. "BRRBBRGGRGRGGRBRGBRRRRRGBBRGBGRBGGGBBGRRBRBGGRGGBG"

    Returns: 275

  47. "RRGRGRRRR"

    Returns: 25

  48. "RGRGRG"

    Returns: 14

  49. "RRRGGBB"

    Returns: 17

  50. "RGRRRGG"

    Returns: 16

  51. "RR"

    Returns: 1

  52. "RGRGRGRGRGRG"

    Returns: 38

  53. "GRGBGB"

    Returns: 14

  54. "RGRRGGRRGG"

    Returns: 29

  55. "RGBGRGRR"

    Returns: 25

  56. "GRRGGRRGRGGBRGBGGGGRBRGGBRBRBRRGGGRBRGRBBGRRRGRRBR"

    Returns: 263

  57. "RRRRBBBB"

    Returns: 18

  58. "RRBGGBBGBRGGRRGBGRBBBGRGBBGGGBRBRG"

    Returns: 183

  59. "RRRGBGRRGBRRGBBRGBGBBGBRBRGBGGRGRGBBGRRGBGBBGBBRGG"

    Returns: 272

  60. "BBBBBBBB"

    Returns: 13

  61. "BBBBBBBBBBR"

    Returns: 19

  62. "RRRRGGGGBBBB"

    Returns: 39


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: