[LeetCode] Two-Letter Card Game

3664. Two-Letter Card Game

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 x in 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int score(vector<string>& cards, char x) {
int ox[10]{0}, xo[10]{0}, oo = 0, lr = 0;
for(auto& c : cards) {
if(c[0] == x and c[1] == x) oo++;
else if(c[0] == x) ox[c[1]-'a']++,lr++;
else if(c[1] == x) xo[c[0]-'a']++,lr++;
}
if(oo >= lr) return lr;

auto helper = [&](int cnt[10]) {
int sum = 0, ma = 0;
for(int i = 0; i < 10; i++) sum += cnt[i], ma = max(ma, cnt[i]);
if(sum < 2) return 0;
return min(sum / 2, sum - ma);
};
int cnt = lr - oo, internal = helper(ox) + helper(xo);
return min(internal, cnt / 2) + oo;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/03/PS/LeetCode/two-letter-card-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.