Problem Statement
Tommy is learning a simple card game called Circle. To play the game, the single player shuffles a deck of cards. He or she then flips through the deck, removing all instances of the 'K' card, and all consecutive pairs of cards that add up to 13. The deck does wrap around, so that if the last card remaining in the deck and the first card remaining in the deck add up to 13, they are both removed. The player keeps cycling through the deck until no more cards can be removed.
Create a class CircleGame containing the method cardsLeft that takes a
'A' - 1 '2' - 2 '3' - 3 '4' - 4 '5' - 5 '6' - 6 '7' - 7 '8' - 8 '9' - 9 'T' - 10 'J' - 11 'Q' - 12 'K' - 13
Given deck, return the number of cards remaining after the game has been played to its fullest such that no more cards can be removed.
Definition
- Class:
- CircleGame
- Method:
- cardsLeft
- Parameters:
- String
- Returns:
- int
- Method signature:
- int cardsLeft(String deck)
- (be sure your method is public)
Notes
- There are no guarantees made about the contents of the deck. For example, there can be more than four of a given value of card.
- Note that if a particular card can make a sum of 13 with the card before or after it, it does not matter which is chosen. For example, 38582, whether you use the first 8 or the second 8, will still become 382 after the two cards (5 and 8) are removed.
Constraints
- deck will have between 10 and 50 characters, inclusive.
- each character of deck will be a character from the set {'2'-'9','A','T','J','Q','K'}.
Examples
"KKKKKKKKKK"
Returns: 0
All K cards are always removed from the deck.
"KKKKKAQT23"
Returns: 1
The K cards are removed, leaving AQT23. AQ are then removed because they add up to 13, leaving T23. Since the deck wraps around and T and 3 add up to 13, they are also removed, just leaving the 2.
"KKKKATQ23J"
Returns: 6
Only the K cards can be removed.
"AT68482AK6875QJ5K9573Q"
Returns: 4
The remaining deck is 6875.
"AQK262362TKKAQ6262437892KTTJA332"
Returns: 24
"KTQ55662JJ3KAAKK2235QKKAK253525KKJKKT"
Returns: 22
"J92J43KT5T879QA2QK3"
Returns: 9
"7879J4JQK24Q46K2A3TQ7T4256632TQ738JA6KA8K959J5T895"
Returns: 42
"A8929JTAQ6J247759TKK5K3K947JQA284Q4256Q36587T6833A"
Returns: 30
"AJJTKK29T883J2JJT9329JAJJTJKK2JTJKQQ88TJ2A"
Returns: 31
"AT26532AQJT87J3Q"
Returns: 0
"AKKKQAATT8AAAQ9A9ATQ"
Returns: 11
"26834658972634985629856986587465872987369535"
Returns: 28
"AQKTQKATQAKTKTQTQTAKTQKATQKATQ"
Returns: 13
"A23456789TJQA23456789TJQ"
Returns: 0
"553553553553"
Returns: 12
"AQTKKKKKKQ"
Returns: 2
"TTTTTTTTTTT3TTT3"
Returns: 12
"4454454454"
Returns: 10
"AKKKKKKKKKT3JJ"
Returns: 3