leetcode

Vowels Game in a String - Link

Question Description

Alice and Bob are playing a game on a string s.

Return true if Alice wins the game, false otherwise.


Constraints


Approach

The key insight is simple:

So, the solution simply checks whether there is at least one vowel in the string.

Why this approach works:

Alternative approaches considered:


Dry Run

Example Input: s = "leetcode"

Step-by-step execution:

Final Answer = true

Example Input: s = "bbcd"

Step-by-step execution:

Final Answer = false


Solution

public class Solution {
    public boolean doesAliceWin(String s) {
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
                ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
                return true;
            }
        }
        return false;
    }
}

Time and Space Complexity