mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
71 lines
3.0 KiB
HTML
71 lines
3.0 KiB
HTML
<p>You are given a deck of cards represented by a string array <code>cards</code>, and each card displays two lowercase letters.</p>
|
|
|
|
<p>You are also given a letter <code>x</code>. You play a game with the following rules:</p>
|
|
|
|
<ul>
|
|
<li>Start with 0 points.</li>
|
|
<li>On each turn, you must find two <strong>compatible</strong> cards from the deck that both contain the letter <code>x</code> in any position.</li>
|
|
<li>Remove the pair of cards and earn <strong>1 point</strong>.</li>
|
|
<li>The game ends when you can no longer find a pair of compatible cards.</li>
|
|
</ul>
|
|
|
|
<p>Return the <strong>maximum</strong> number of points you can gain with optimal play.</p>
|
|
|
|
<p>Two cards are <strong>compatible</strong> if the strings differ in <strong>exactly</strong> 1 position.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">cards = ["aa","ab","ba","ac"], x = "a"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>On the first turn, select and remove cards <code>"ab"</code> and <code>"ac"</code>, which are compatible because they differ at only index 1.</li>
|
|
<li>On the second turn, select and remove cards <code>"aa"</code> and <code>"ba"</code>, which are compatible because they differ at only index 0.</li>
|
|
</ul>
|
|
|
|
<p>Because there are no more compatible pairs, the total score is 2.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">cards = ["aa","ab","ba"], x = "a"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<ul>
|
|
<li>On the first turn, select and remove cards <code>"aa"</code> and <code>"ba"</code>.</li>
|
|
</ul>
|
|
|
|
<p>Because there are no more compatible pairs, the total score is 1.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">cards = ["aa","ab","ba","ac"], x = "b"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>The only cards that contain the character <code>'b'</code> are <code>"ab"</code> and <code>"ba"</code>. However, they differ in both indices, so they are not compatible. Thus, the output is 0.</p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>2 <= cards.length <= 10<sup>5</sup></code></li>
|
|
<li><code>cards[i].length == 2</code></li>
|
|
<li>Each <code>cards[i]</code> is composed of only lowercase English letters between <code>'a'</code> and <code>'j'</code>.</li>
|
|
<li><code>x</code> is a lowercase English letter between <code>'a'</code> and <code>'j'</code>.</li>
|
|
</ul>
|