3664. Two-Letter Card Game Medium

@problem@discussion
#Array#Hash Table#String#Counting#Enumeration



1/**
2 * [3664] Two-Letter Card Game
3 *
4 * You are given a deck of cards represented by a string array cards, and each card displays two lowercase letters.
5 * You are also given a letter x. You play a game with the following rules:
6 * 
7 * 	Start with 0 points.
8 * 	On each turn, you must find two compatible cards from the deck that both contain the letter x in any position.
9 * 	Remove the pair of cards and earn 1 point.
10 * 	The game ends when you can no longer find a pair of compatible cards.
11 * 
12 * Return the maximum number of points you can gain with optimal play.
13 * Two cards are compatible if the strings differ in exactly 1 position.
14 *  
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">cards = ["aa","ab","ba","ac"], x = "a"</span>
18 * Output: <span class="example-io">2</span>
19 * Explanation:
20 * 
21 * 	On the first turn, select and remove cards "ab" and "ac", which are compatible because they differ at only index 1.
22 * 	On the second turn, select and remove cards "aa" and "ba", which are compatible because they differ at only index 0.
23 * 
24 * Because there are no more compatible pairs, the total score is 2.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">cards = ["aa","ab","ba"], x = "a"</span>
29 * Output: <span class="example-io">1</span>
30 * Explanation:
31 * 
32 * 	On the first turn, select and remove cards "aa" and "ba".
33 * 
34 * Because there are no more compatible pairs, the total score is 1.
35 * </div>
36 * <strong class="example">Example 3:
37 * <div class="example-block">
38 * Input: <span class="example-io">cards = ["aa","ab","ba","ac"], x = "b"</span>
39 * Output: <span class="example-io">0</span>
40 * Explanation:
41 * 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.
42 * </div>
43 *  
44 * Constraints:
45 * 
46 * 	2 <= cards.length <= 10^5
47 * 	cards[i].length == 2
48 * 	Each cards[i] is composed of only lowercase English letters between 'a' and 'j'.
49 * 	x is a lowercase English letter between 'a' and 'j'.
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/two-letter-card-game/
55// discuss: https://leetcode.com/problems/two-letter-card-game/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn score(cards: Vec<String>, x: char) -> i32 {
61        0
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3664() {
73    }
74}
75

Back
© 2026 bowen.ge All Rights Reserved.