Problem Statement
You are playing a game with a deck of cards, half red and half black. After your friend thoroughly shuffles the cards, you cut the deck. Your friend then begins to turn the cards over one at a time, starting from the top of the deck. If, at any time, there are more red cards showing than black cards, you lose. If this never occurs by they time all the cards have been turned up, you win.
After losing several times in a row, you get frustrated and decide to cheat. After your friend shuffles the deck, you distract him and then secretly look at the cards, to figure out where you should cut the deck.
"Cutting the deck" means to take a stack of cards off the top of the deck, and place this stack on the bottom.
Given a
Definition
- Class:
- BlackAndRed
- Method:
- cut
- Parameters:
- String
- Returns:
- int
- Method signature:
- int cut(String deck)
- (be sure your method is public)
Constraints
- cards will contain between 2 and 50 characters, inclusive.
- cards will contain only the characters 'R' and 'B'.
- cards will contain the same number of 'R' and 'B' characters.
Examples
"BRBRBR"
Returns: 0
You could cut the deck at positions 0, 2, or 4 to win.
"RBRBRB"
Returns: 1
If you cut the deck at position 0, the first card dealt will be red and you will lose immediately. However, if you cut the deck at position 1, you will win.
"BBBRRRRB"
Returns: 7
The only way to win is to cut the deck at position 7. After this cut, the deck becomes: "BBBBRRRR".
"BR"
Returns: 0
"RBRBBRRRRBBBRBBRRBRBBRRRBRBBBRBRBRBRBRRB"
Returns: 9
"RBRBRBRBRBRBRBRBRBRBRRBBRBRBBRBRBBRRBRBRBRBRRBRBRB"
Returns: 22
"RBRRBBRBRBBRBRBBRRBRBRRRBBRBBRBRBRBRBRBRBRBRRBRBRB"
Returns: 4
"BBBRRBRBRRRBRBRBRBBRBRBRRBRBBRBRBRBRBRBRBRBRRBRBRB"
Returns: 11
"BBBBBRBRBRBRBRRRRBRBRBRBBRBRBRRRBRBRBRBRBRBRRBRBRB"
Returns: 45
"RRRRRBRRRBBRBRBRBBRBRBBBBBRBRBRBRBRRBRBRBBRBBRRB"
Returns: 9
"BRBBRBRBBBBBRBRBRBRBRRBRRRRRRBRRRBBRBRBBRBBRRB"
Returns: 33
"RRRRRRRRRRRRRRRRRRRRRRRRRBBBBBBBBBBBBBBBBBBBBBBBBB"
Returns: 25
"BBBBBBBBBBBBBBBBBBBBBBBBBRRRRRRRRRRRRRRRRRRRRRRRRR"
Returns: 0
"RRBRBB"
Returns: 2
"BBRBRR"
Returns: 0
"RBBRRBBRRBBRRB"
Returns: 1
"BRRBBRRBBRRBBR"
Returns: 3
"BBBBBBBBBBBBBBBBBBBBBBBBRRRRRRRRRRRRRRRRRRRRRRRRRB"
Returns: 49
"BRBRBR"
Returns: 0
"BBBRRRRB"
Returns: 7
"BRRRRBBBBR"
Returns: 5