Snapshot Summary 2038 - If two adjacent colors are the same, delete the current color

Directory link:

Likou Programming Questions - Solution Summary - Sharing + Recording - CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Original title link: force buckle


describe:

There are a total of n color fragments in a row, each of which is either 'A' or 'B' . gives you a string colors of length n, where colors[i] represents the color of the ith color fragment.

Alice and Bob are playing a game where they take turns removing colors from this string. Alice starts.

If a color fragment is 'A' and two adjacent colors are both color 'A', then Alice can delete the color fragment. Alice MUST NOT delete any color 'B' segments.
If a color fragment is 'B' and two adjacent colors are both color 'B', then Bob can delete the color fragment. Bob may not delete any segments of color 'A'.
Alice and Bob cannot remove color fragments from both ends of the string.
If one of them cannot continue, that player loses the game and the other player wins.
Assuming both Alice and Bob adopt the optimal strategy, return true if Alice wins, otherwise return false if Bob wins.

Example 1:

Input: colors = "AAABABB"
Output: true
Explanation:
AAABABB -> AABABB
Alice operates first.
She deletes the second 'A' from the left, which is also the only 'A' where the adjacent color segments are all 'A's.

Now it's Bob's turn.
Bob can't do anything because there is no color fragment 'B' with both 'B' adjacent.
Therefore, Alice wins, returning true.
Example 2:

Input: colors = "AA"
Output: false
Explanation:
Alice operates first.
There are only 2 'A's and they are both ends of the string, so she can't do anything.
So Bob wins and returns false.
Example 3:

Input: colors = "ABBBBBBBAAA"
Output: false
Explanation:
ABBBBBBAAA -> ABBBBBBBAA
Alice operates first.
Her only option is to delete the second 'A' from the right.

ABBBBBBBAA -> ABBBBBBAA
Next is Bob's turn.
He has many options, he can choose any 'B' to delete.

Then it's Alice's turn, she can't delete any fragments.
So Bob wins and returns false.
 

hint:

1 <= colors.length <= 105
colors contain only letters 'A' and 'B'

Source: LeetCode
Link: https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color The
copyright belongs to Leetcode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Problem solving ideas:

* Problem solving ideas:
* To put it simply, it depends on whether A satisfies the condition more or B satisfies the condition more.
* The number of satisfying conditions depends on the total number of consecutive A or B. For example, when A exceeds 3 consecutively, aSum+(aNum-2).
* Of course, you also need to calculate the number of aSum and bSum at the end

Code:

public boolean winnerOfGame(String colors) {
        int aSum = 0;
        int bSum = 0;

        int aNum = 0;
        int bNum = 0;

        for (char c : colors.toCharArray()) {
            if (c == 'A') {
                if (bNum >= 3) {
                    bSum += (bNum - 2);
                }
                bNum = 0;
                aNum++;
                continue;
            }
            if (aNum >= 3) {
                aSum += (aNum - 2);
            }
            bNum++;
            aNum = 0;
        }
        if (bNum >= 3) {
            bSum += (bNum - 2);
        }
        if (aNum >= 3) {
            aSum += (aNum - 2);
        }
        return aSum > bSum;
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324110459&siteId=291194637