Two-Letter Card Game Problem
Description
LeetCode Problem 3664.
You are given a deck of cards represented by a string array cards, and each card displays two lowercase letters.
You are also given a letter x. You play a game with the following rules:
- Start with 0 points.
- On each turn, you must find two compatible cards from the deck that both contain the letter
xin any position. - Remove the pair of cards and earn 1 point.
- The game ends when you can no longer find a pair of compatible cards.
Return the maximum number of points you can gain with optimal play.
Two cards are compatible if the strings differ in exactly 1 position.
Example 1:
Input: cards = ["aa","ab","ba","ac"], x = "a"
Output: 2
Explanation:
- On the first turn, select and remove cards "ab" and "ac", which are compatible because they differ at only index 1.
- On the second turn, select and remove cards "aa" and "ba", which are compatible because they differ at only index 0.
Because there are no more compatible pairs, the total score is 2.
Example 2:
Input: cards = ["aa","ab","ba"], x = "a"
Output: 1
Explanation:
- On the first turn, select and remove cards "aa" and "ba".
Because there are no more compatible pairs, the total score is 1.
Example 3:
Input: cards = ["aa","ab","ba","ac"], x = "b"
Output: 0
Explanation:
The only cards that contain the character 'b' are "ab" and "ba". However, they differ in both indices, so they are not compatible. Thus, the output is 0.
Constraints:
2 <= cards.length <= 10^5cards[i].length == 2- Each
cards[i]is composed of only lowercase English letters between'a'and'j'. xis a lowercase English letter between'a'and'j'.
Solution
Approach: Counting and Greedy
Only cards containing x matter. They fall into three kinds: "xx", "x?" (with ? different from x) and "?x". Two cards are compatible when they differ in exactly one position, which gives these rules:
- Two
"x?"cards are compatible when their other letters differ; likewise for two"?x"cards. - An
"x?"card is never compatible with a"?x"card. "xx"is compatible with every"x?"and"?x"card, but not with another"xx".
For "x?" cards (and separately for "?x" cards), let total be their number and mx the largest count of one letter. We can form min(total / 2, total - mx) pairs of different letters; what is left over is "spare".
Each "xx" card is best used with a spare card, giving one point each. If "xx" cards remain, every two of them can take apart one existing pair and pair with both of its cards, turning one point into two, so we gain one more point per two "xx" cards, up to the number of pairs.
Complexity: time O(n), space O(1).
class Solution:
def score(self, cards: List[str], x: str) -> int:
both = 0
left = Counter()
right = Counter()
for s in cards:
if s[0] == x and s[1] == x:
both += 1
elif s[0] == x:
left[s[1]] += 1
elif s[1] == x:
right[s[0]] += 1
pairs_total = 0
spare = 0
for g in (left, right):
total = sum(g.values())
mx = max(g.values(), default=0)
pairs = min(total // 2, total - mx)
pairs_total += pairs
spare += total - 2 * pairs
use = min(both, spare)
both -= use
# two leftover xx cards can turn one pair of other cards into two points
return pairs_total + use + min(both // 2, pairs_total)