Problem Statement
You have a deck of 52 cards. Each card has a single uppercase English letter ('A'-'Z'). Each letter is used exactly twice, so the deck contains 26 pairs of cards.
You are going to use the cards to play the memory game with your friends. You shuffled the deck of cards and placed them onto the table. The cards are arranged into a single row and they are all face-down (i.e., the letters on them are not visible).
The memory game is played in turns. In each turn, the active player starts by selecting one of the cards and turning it over. After seeing the letter on this card, the player then selects and turns over another card. If the two cards are distinct, they are turned face down again and the turn ends. If they form a pair, the player scores a point and takes those cards away from the table. Then, the player gets to repeat the whole process until they either reveal two cards that don't match, or find and remove the last pair of cards.
Some (possibly zero) turns of the game have already been played by other players. It is now your turn to play the game. There are 2*N cards still on the table.
(Note that during the game only pairs of matching cards are removed from the game, and thus it is guaranteed that those 2*N cards contain N pairs of matching cards.)
The
What is the maximum number of pairs you are guaranteed to find this turn, if you play the game optimally (i.e., in a way that maximizes this number)? Calculate and return this number of pairs.
Definition
- Class:
- OptimalMemoryGame
- Method:
- findPairs
- Parameters:
- String
- Returns:
- int
- Method signature:
- int findPairs(String known)
- (be sure your method is public)
Notes
- We want to stress that the two cards selected by the player are not selected at the same time. Instead, they are selected and revealed one at a time. When selecting the second card ("looking for a pair"), the player already knows what letter was on the first card they selected, and this information can influence the choice of the second card.
Constraints
- N will be between 1 and 26, inclusive.
- known will contain exactly 2*N characters.
- Each character in known will be an uppercase English letter or a dash.
- Each letter will appear at most twice in known.
- There will be at most N distinct letters in known.
Examples
"AEIOUIAEUO"
Returns: 5
All ten remaining cards have already been revealed and you know what they are. You can simply collect all pairs, one after another.
"----------"
Returns: 0
The best you can do is flip some pair of cards. You aren't guaranteed that they will match. (In fact, it's likely that they won't.) Thus, you aren't guaranteed to find any pairs here, regardless of what strategy you use.
"--------A----E-----IAO----U---"
Returns: 1
The pair of As is the only pair you can collect here. After you do so, you may get lucky and find another pair, but this isn't guaranteed to happen.
"-X"
Returns: 1
Sometimes we can guarantee to find a pair even if we aren't actually given the positions of both cards in it. For example, here the only unknown card is clearly the second X, and once we know that, we can guarantee that we can find and collect a matching pair.
"--"
Returns: 1
"-A-E-I-O-U"
Returns: 5
"-A-E-I-O-U--"
Returns: 0
"-A-EAIEOIUOU"
Returns: 6
"RAR-SES-TIT-VOV-WUW-"
Returns: 10
"DZOQ---G--EK-M--X-NVC-I-Y--WBR-ALJ----HS----T-PU--F-"
Returns: 26
"T----FH----R---Y---S-OKVP-W--X-CLDH-UI-EGMZ-JBNQ--A-"
Returns: 26
"LG-KICJ-Q---DT-O--XBVESH-BPIOFFYMTXJN-VLAW-DURMUY-Z-"
Returns: 26
"KQASIDLTLTUIRHOXN-WGSJRGOEP-NUMFEWCBPDHCJVMQVFYAYXZK"
Returns: 26
"YYUBOWSFZNVBTNRVGJPLXCQJTIGIMHAOXADKEKREMWLFSDCPZHUQ"
Returns: 26
"HXIGSSNZ-TXGPBYOLH-JDMJLAPMNRKYUTWZCOREVBCKUVQAWQIDE"
Returns: 26
"VWWZB-KAORQSRPYOFTLD-TISQJNCJUHMYKLDBMVEEZHFPCXNAIX-"
Returns: 24
"ABZVAQCKPVU-LTBIHYGWJNDSFZDFOKOWXYCU-PGX-QHEIML-METN"
Returns: 23
"IKGFLWEMAHL-JARCPESPHD-XOOWBIRNVTUBVTF-MXNKGSDYC-YUJ"
Returns: 24
"----------------------------------------------------"
Returns: 0
"D--I--OKQ-"
Returns: 5
"-FFQ--Q-"
Returns: 2
"RUZTRT-ZUD"
Returns: 5
"THR-TH"
Returns: 3
"IF--FW-IW-"
Returns: 3
"K---K-----"
Returns: 1
"HFJ--O---J"
Returns: 1
"NS---ME-EQ"
Returns: 5
"------X-"
Returns: 0
"W-GWI--C"
Returns: 4
"H-PHP-"
Returns: 3
"--Q--F-YI-"
Returns: 0
"----J-"
Returns: 0
"-----D"
Returns: 0
"-C-DND"
Returns: 3
"---Y-F-XAB"
Returns: 5
"-NY-H-"
Returns: 3
"--H-----"
Returns: 0
"-V-T--BH"
Returns: 4
"RXRDXKKD"
Returns: 4
"H--H----TL"
Returns: 1
"E-HHE-XRRX"
Returns: 5
"-E-G-LV-"
Returns: 4
"PPYHY-"
Returns: 3
"-O-EUOXE-X"
Returns: 3
"NS-F-FSAV-"
Returns: 5
"DDI-P-"
Returns: 3
"--I-----"
Returns: 0
"-GX-SL-H--"
Returns: 5
"--ZKZE"
Returns: 3
"--D-Y-GA-EVD-YUCF-ZNT--N"
Returns: 12
"----------Q---J-B--F"
Returns: 0
"--GZKGT-MMWDOKDOEZWIET"
Returns: 9
"--P--R--I--Q--ZX-PO--M-CSF--K-"
Returns: 1
"SRZ--N--JQAX-----SFR---I----Y--W"
Returns: 2
"D-AELH-----AREM-YGDH"
Returns: 4
"------------------------"
Returns: 0
"G-A----J-O-----N-A-X-S-L"
Returns: 1
"---K--AFV---------KGZ---HHLL"
Returns: 3
"CGYH-V-A----GH---------P-K-MSM----"
Returns: 3
"---KC-AGIO-P-Z---J-NEW--T-M-HB---RD--L"
Returns: 19
"-B-Y--ZRXGP-F----U-M-B--"
Returns: 1
"-----N---BBVOQ--W-----QC-----V------"
Returns: 3
"J--X---LG-TW--TP-WS-----F-KF-J"
Returns: 4
"-V-Y---N--VC-SM-C-S--N"
Returns: 4
"SOU-W-O----QVWSYVJT--N-H"
Returns: 4
"---------D--R-----H----------R------A-"
Returns: 1
"T--Q-----J-EOA--K-NEA-M-Y----Z-N-----Q"
Returns: 4
"-S---PK--S-----A--K-A-P---"
Returns: 4
"--P-UI-------RF-R--YMECTNL-ZG-"
Returns: 1
"W-VS-O-CQUJBICWJSXHB-PHNPKFIDNKMZYXVYQRGDTO-TZGMUR"
Returns: 22
"---I---G-Y--BNN-CXJIK--AR-Q--OV-JT--WPMU--"
Returns: 3
"--A-W-Y---I--K--N---D-O------LV-D---QC-JRXQ-F-RH"
Returns: 3
"Z-I-V-L-NI--B----NW-SDJBK-WXFF----R--TGCOH-E----"
Returns: 5
"IW-YKV-TEZ-U---D--C--SXOLAUXFH-FM-B-J--Y---B--"
Returns: 5
"OA-----R---C-DI--R--------OIPDF-L-H--B-SA---"
Returns: 5
"-W-J---FQA-ZO-M-B-WRIYLUX-KN-V--S-R-E-PZB-"
Returns: 21
"--I-------P---X-M----RMZ------DS--Q--------U----C---"
Returns: 1
"V-OF--ZD-T-C-Q---RJ----W--LBOI-PYASKU-E-S----H-X"
Returns: 2
"M-OCT--ILFGHUUWRA-K-CX-INYANVSPBJTO--FM-PVXRHL"
Returns: 15
"U-YH-LF-CVA----WQG----M-F-D-OT-N-AEIKZ-J---J"
Returns: 3
"--Z-O--WWESZ--T-YK-G-----R--OIVIEHKGT-RQF--AASQ-X--Y"
Returns: 13
"-Q-O---JH-SZY-CQ--YT-G-----BLN-ED----V--"
Returns: 2
"IGO-ARTAG-F-QDLS-KH-ZQ-ZJ-S-FDUXBI-UB-X---KVJHYTRYLP"
Returns: 18
"-WJ--O-L--XF-GK-MV--E----FH-I-A-ZP--S-M--Y---TCY"
Returns: 3
"-GG----FMN-X----IDTIX-W---MY--VZNW--F-C---"
Returns: 7
"G--R-DJ---OQAWZ---H--UB--FK-MS--X-P--Y--N-L-CI-E"
Returns: 24
"A-BEIW---LV-C-PMUEDQ-TR--PO-OSIHJV---BDZ-ZFG--"
Returns: 8
"-F--T-T--M--R-EN-ZR-M-H-PJ--E----F----------VG"
Returns: 5
"-EOAUIS-Y-K-PZAQ-QSCDLBZYP-RTHEIURF--DXL--XBT-----KO"
Returns: 17
"A--A"
Returns: 2
"-WA-"
Returns: 2
"--EX-DDY-ACE"
Returns: 6
"ABCD---D"
Returns: 4
"AABBCDE---"
Returns: 5
"BB--"
Returns: 2
"ABCD----"
Returns: 4
"-ZZAB-BA"
Returns: 4
"--AABBCC"
Returns: 4
"AB--"
Returns: 2
"--AB"
Returns: 2
"ABC---"
Returns: 3
"A-B-BA"
Returns: 3
"AAIO-K--"
Returns: 4
"AA--"
Returns: 2
"Y--X"
Returns: 2
"ABCAB--C"
Returns: 4
"AABBCCDDEEFFGG--IIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ"
Returns: 26
"----ABCD"
Returns: 4
"AABC--"
Returns: 3
"AABBCCDD----"
Returns: 4
"AZ--"
Returns: 2
"AABCDEF-----"
Returns: 6
"ABCDE-----"
Returns: 5
"----AABB"
Returns: 2