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
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
"RGB"
Returns: 3
Any strategy is optimal here. Each strategy scores 0+1+2 = 3 points.
"RGGRBBB"
Returns: 21
"RRRGBRR"
Returns: 16
"RRRR"
Returns: 5
"GGRRRGR"
Returns: 18
"G"
Returns: 0
"RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"
Returns: 97
"RRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG"
Returns: 144
"RRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGBBBBBBBBBBBBBBBBBB"
Returns: 195
"RRGGBBRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"
Returns: 279
"RGRBGRBRGGRBRGRGGBRGRB"
Returns: 110
"G"
Returns: 0
"B"
Returns: 0
"RGGGRGGRGRGGRGRGRGBBB"
Returns: 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GGGBGGRRBRGRBBGBGBBRGGGGBBRR"
Returns: 155
"GBGGBRBBGGBRRRRGRGRGGRGGGRBRRRGRRGRBBGBRRBBRRGBGG"
Returns: 265
"RBBRBGRGGBBBRBBGBRGBRBRGGRRBBBRBGGRGGRGBBBRGRGBR"
Returns: 264
"BRGRGRBBBGRBRBR"
Returns: 68
"GBGBBBGRGBBBBRG"
Returns: 58
"BGRRGGRRRGRBBBB"
Returns: 63
"RGGRGRGRRBRGGBRBGRBRB"
Returns: 92
"BGBBBBBBBBBBBBBBBBBBBBBBBBBBRGBGBGBGBRRGBGBRGBGBRR"
Returns: 197
"GGGBGGGRRRRRRRRRBGGGGGGGGGRRRRRRRRRRBBBBGGGRRBGRGR"
Returns: 259
"GGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"
Returns: 154
"RBRBGGGGG"
Returns: 33
"RRRGGGBBB"
Returns: 27
"RGBRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"
Returns: 142
"RBBBBRGGGGGRRRGB"
Returns: 69
"BRRBBRGGRGRGGRBRGBRRRRRGBBRGBGRBGGGBBGRRBRBGGRGGBG"
Returns: 275
"RRGRGRRRR"
Returns: 25
"RGRGRG"
Returns: 14
"RRRGGBB"
Returns: 17
"RGRRRGG"
Returns: 16
"RR"
Returns: 1
"RGRGRGRGRGRG"
Returns: 38
"GRGBGB"
Returns: 14
"RGRRGGRRGG"
Returns: 29
"RGBGRGRR"
Returns: 25
"GRRGGRRGRGGBRGBGGGGRBRGGBRBRBRRGGGRBRGRBBGRRRGRRBR"
Returns: 263
"RRRRBBBB"
Returns: 18
"RRBGGBBGBRGGRRGBGRBBBGRGBBGGGBRBRG"
Returns: 183
"RRRGBGRRGBRRGBBRGBGBBGBRBRGBGGRGRGBBGRRGBGBBGBBRGG"
Returns: 272
"BBBBBBBB"
Returns: 13
"BBBBBBBBBBR"
Returns: 19
"RRRRGGGGBBBB"
Returns: 39