3227. Vowels Game in a String #2164
-
Topics: Alice and Bob are playing a game on a string. You are given a string
The first player who cannot make a move on their turn loses the game. We assume that both Alice and Bob play optimally. Return The English vowels are: Example 1:
Example 2:
Constraints:
Hint:
Footnotes |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We are going to solve the problem by analyzing the game theory properties of the string based on the count of vowels. Approach:
Let's implement this solution in PHP: 3227. Vowels Game in a String <?php
/**
* @param String $s
* @return Boolean
*/
function doesAliceWin($s) {
$vowels = "aeiou";
for ($i = 0; $i < strlen($s); $i++) {
if (strpos($vowels, $s[$i]) !== false) {
return true;
}
}
return false;
}
// Test cases
var_dump(doesAliceWin("leetcoder")); // true
var_dump(doesAliceWin("bbcd")); // false
var_dump(doesAliceWin("a")); // true
var_dump(doesAliceWin("ae")); // true
var_dump(doesAliceWin("zzz")); // false
?> Explanation:
This approach efficiently determines the winner by leveraging the presence of vowels, ensuring optimal performance with a linear scan of the string. The time complexity is O(n), where n is the length of the string. |
Beta Was this translation helpful? Give feedback.
We are going to solve the problem by analyzing the game theory properties of the string based on the count of vowels.
Approach:
t…