LeetCode 3227: Vowels Game in a String Solution

Master LeetCode problem 3227 (Vowels Game in a String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3227. Vowels Game in a String

Problem Explanation

Explanation

To solve this problem, we can use a simple approach where we iterate through the string s and count the number of vowels in each substring. Based on the count of vowels in each substring, we can determine if the substring has an odd or even number of vowels.

We can then simulate the game played by Alice and Bob. Alice will try to remove substrings with odd numbers of vowels, while Bob will try to remove substrings with even numbers of vowels. We need to check if Alice can make a move on her turn. If Alice can make a move and Bob cannot make a move, then Alice wins the game.

Solution Code

public boolean canAliceWin(String s) {
    int n = s.length();
    boolean aliceTurn = true;
    int vowelsCount = 0;

    for (char c : s.toCharArray()) {
        if ("aeiou".indexOf(c) >= 0) {
            vowelsCount++;
        } else {
            if (vowelsCount % 2 == 1) {
                if (aliceTurn) {
                    return true;
                }
                aliceTurn = !aliceTurn;
            }
            vowelsCount = 0;
        }
    }

    return aliceTurn && vowelsCount % 2 == 1;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3227 (Vowels Game in a String)?

This page provides optimized solutions for LeetCode problem 3227 (Vowels Game in a String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3227 (Vowels Game in a String)?

The time complexity for LeetCode 3227 (Vowels Game in a String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3227 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3227 in Java, C++, or Python.

Back to LeetCode Solutions