Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
x in any position.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:
"ab" and "ac", which are compatible because they differ at only index 1."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:
"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 <= 105cards[i].length == 2cards[i] is composed of only lowercase English letters between 'a' and 'j'.x is a lowercase English letter between 'a' and 'j'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
["aa","ab","ba","ac"] "a"
["aa","ab","ba"] "a"
["aa","ab","ba","ac"] "b"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3664: Two-Letter Card Game
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3664: Two-Letter Card Game
// package main
//
// // https://space.bilibili.com/206214
// // 计算这一组的得分(配对个数),以及剩余元素个数
// func calc(cnt []int, x byte) (int, int) {
// sum, mx := 0, 0
// for i, c := range cnt {
// if i != int(x-'a') {
// sum += c
// mx = max(mx, c)
// }
// }
// pairs := min(sum/2, sum-mx)
// return pairs, sum - pairs*2
// }
//
// func score(cards []string, x byte) int {
// var cnt1, cnt2 [10]int
// for _, s := range cards {
// if s[0] == x {
// cnt1[s[1]-'a']++
// } else if s[1] == x {
// cnt2[s[0]-'a']++
// }
// }
//
// pairs1, left1 := calc(cnt1[:], x)
// pairs2, left2 := calc(cnt2[:], x)
// ans := pairs1 + pairs2 // 不考虑 xx 时的得分
//
// cntXX := cnt1[x-'a']
// // 把 xx 和剩下的 x? 和 ?x 配对
// // 每有 1 个 xx,得分就能增加一,但这不能超过剩下的 x? 和 ?x 的个数 left1+left2
// if cntXX > 0 {
// mn := min(cntXX, left1+left2)
// ans += mn
// cntXX -= mn
// }
//
// // 如果还有 xx,就撤销之前的配对,比如 (ax,bx) 改成 (ax,xx) 和 (bx,xx)
// // 每有 2 个 xx,得分就能增加一,但这不能超过之前的配对个数 pairs1+pairs2
// // 由于这种方案平均每个 xx 只能增加 0.5 分,不如上面的,所以先考虑把 xx 和剩下的 x? 和 ?x 配对,再考虑撤销之前的配对
// if cntXX > 0 {
// ans += min(cntXX/2, pairs1+pairs2)
// }
//
// return ans
// }
// Accepted solution for LeetCode #3664: Two-Letter Card Game
package main
// https://space.bilibili.com/206214
// 计算这一组的得分(配对个数),以及剩余元素个数
func calc(cnt []int, x byte) (int, int) {
sum, mx := 0, 0
for i, c := range cnt {
if i != int(x-'a') {
sum += c
mx = max(mx, c)
}
}
pairs := min(sum/2, sum-mx)
return pairs, sum - pairs*2
}
func score(cards []string, x byte) int {
var cnt1, cnt2 [10]int
for _, s := range cards {
if s[0] == x {
cnt1[s[1]-'a']++
} else if s[1] == x {
cnt2[s[0]-'a']++
}
}
pairs1, left1 := calc(cnt1[:], x)
pairs2, left2 := calc(cnt2[:], x)
ans := pairs1 + pairs2 // 不考虑 xx 时的得分
cntXX := cnt1[x-'a']
// 把 xx 和剩下的 x? 和 ?x 配对
// 每有 1 个 xx,得分就能增加一,但这不能超过剩下的 x? 和 ?x 的个数 left1+left2
if cntXX > 0 {
mn := min(cntXX, left1+left2)
ans += mn
cntXX -= mn
}
// 如果还有 xx,就撤销之前的配对,比如 (ax,bx) 改成 (ax,xx) 和 (bx,xx)
// 每有 2 个 xx,得分就能增加一,但这不能超过之前的配对个数 pairs1+pairs2
// 由于这种方案平均每个 xx 只能增加 0.5 分,不如上面的,所以先考虑把 xx 和剩下的 x? 和 ?x 配对,再考虑撤销之前的配对
if cntXX > 0 {
ans += min(cntXX/2, pairs1+pairs2)
}
return ans
}
# Accepted solution for LeetCode #3664: Two-Letter Card Game
# Time: O(n + 26)
# Space: O(26)
# freq table, math
class Solution(object):
def score(self, cards, x):
cnt1 = [0]*26
cnt2 = [0]*26
cnt3 = 0
for s in cards:
if x not in s:
continue
if s[0] == x == s[1]:
cnt3 += 1
elif s[0] == x:
cnt1[ord(s[1])-ord('a')] += 1
elif s[1] == x:
cnt2[ord(s[0])-ord('a')] += 1
total1, total2 = sum(cnt1), sum(cnt2)
mx1, mx2 = max(cnt1), max(cnt2)
pair1, pair2 = min(total1-mx1, total1//2), min(total2-mx2, total2//2)
pair3 = min((total1-2*pair1)+(total2-2*pair2), cnt3)
return (pair1+pair2)+pair3+min(pair1+pair2, (cnt3-pair3)//2)
// Accepted solution for LeetCode #3664: Two-Letter Card Game
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3664: Two-Letter Card Game
// package main
//
// // https://space.bilibili.com/206214
// // 计算这一组的得分(配对个数),以及剩余元素个数
// func calc(cnt []int, x byte) (int, int) {
// sum, mx := 0, 0
// for i, c := range cnt {
// if i != int(x-'a') {
// sum += c
// mx = max(mx, c)
// }
// }
// pairs := min(sum/2, sum-mx)
// return pairs, sum - pairs*2
// }
//
// func score(cards []string, x byte) int {
// var cnt1, cnt2 [10]int
// for _, s := range cards {
// if s[0] == x {
// cnt1[s[1]-'a']++
// } else if s[1] == x {
// cnt2[s[0]-'a']++
// }
// }
//
// pairs1, left1 := calc(cnt1[:], x)
// pairs2, left2 := calc(cnt2[:], x)
// ans := pairs1 + pairs2 // 不考虑 xx 时的得分
//
// cntXX := cnt1[x-'a']
// // 把 xx 和剩下的 x? 和 ?x 配对
// // 每有 1 个 xx,得分就能增加一,但这不能超过剩下的 x? 和 ?x 的个数 left1+left2
// if cntXX > 0 {
// mn := min(cntXX, left1+left2)
// ans += mn
// cntXX -= mn
// }
//
// // 如果还有 xx,就撤销之前的配对,比如 (ax,bx) 改成 (ax,xx) 和 (bx,xx)
// // 每有 2 个 xx,得分就能增加一,但这不能超过之前的配对个数 pairs1+pairs2
// // 由于这种方案平均每个 xx 只能增加 0.5 分,不如上面的,所以先考虑把 xx 和剩下的 x? 和 ?x 配对,再考虑撤销之前的配对
// if cntXX > 0 {
// ans += min(cntXX/2, pairs1+pairs2)
// }
//
// return ans
// }
// Accepted solution for LeetCode #3664: Two-Letter Card Game
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3664: Two-Letter Card Game
// package main
//
// // https://space.bilibili.com/206214
// // 计算这一组的得分(配对个数),以及剩余元素个数
// func calc(cnt []int, x byte) (int, int) {
// sum, mx := 0, 0
// for i, c := range cnt {
// if i != int(x-'a') {
// sum += c
// mx = max(mx, c)
// }
// }
// pairs := min(sum/2, sum-mx)
// return pairs, sum - pairs*2
// }
//
// func score(cards []string, x byte) int {
// var cnt1, cnt2 [10]int
// for _, s := range cards {
// if s[0] == x {
// cnt1[s[1]-'a']++
// } else if s[1] == x {
// cnt2[s[0]-'a']++
// }
// }
//
// pairs1, left1 := calc(cnt1[:], x)
// pairs2, left2 := calc(cnt2[:], x)
// ans := pairs1 + pairs2 // 不考虑 xx 时的得分
//
// cntXX := cnt1[x-'a']
// // 把 xx 和剩下的 x? 和 ?x 配对
// // 每有 1 个 xx,得分就能增加一,但这不能超过剩下的 x? 和 ?x 的个数 left1+left2
// if cntXX > 0 {
// mn := min(cntXX, left1+left2)
// ans += mn
// cntXX -= mn
// }
//
// // 如果还有 xx,就撤销之前的配对,比如 (ax,bx) 改成 (ax,xx) 和 (bx,xx)
// // 每有 2 个 xx,得分就能增加一,但这不能超过之前的配对个数 pairs1+pairs2
// // 由于这种方案平均每个 xx 只能增加 0.5 分,不如上面的,所以先考虑把 xx 和剩下的 x? 和 ?x 配对,再考虑撤销之前的配对
// if cntXX > 0 {
// ans += min(cntXX/2, pairs1+pairs2)
// }
//
// return ans
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.