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.

Example 1
Inputcards = ["aa","ab","ba","ac"], x = "a"
Output2
Select and remove "ab" with "ac", then "aa" with "ba", for a total score of 2.
Example 2
Inputcards = ["aa","ab","ba"], x = "a"
Output1
Selecting and removing "aa" and "ba" gives 1 point, after which no more compatible pairs remain.

Constraints

  • 2 <= cards.length <= 10^5
  • cards[i].length == 2
  • Each cards[i] is composed of only lowercase English letters between 'a' and 'j'.
  • x is a lowercase English letter between 'a' and 'j'.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate