Skip to content

Commit 2e1e553

Browse files
authored
Create 3227-vowels-game-in-a-string.java
1 parent 0ea9b3e commit 2e1e553

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Problem: 3227. Vowels Game in a String
3+
*
4+
* Alice and Bob are playing a game on a string s.
5+
* - Alice removes any non-empty substring containing an odd number of vowels.
6+
* - Bob removes any non-empty substring containing an even number of vowels.
7+
* The game starts with Alice and they take turns optimally.
8+
* The player who cannot make a move loses.
9+
*
10+
* Return true if Alice wins the game, false otherwise.
11+
*
12+
* Approach:
13+
* The key insight is simple:
14+
* - If there is at least one vowel in the string, Alice can remove a substring containing exactly one vowel.
15+
* - This leaves Bob in a position where the vowel count is now reduced by one.
16+
* - Since both play optimally, Alice can always force a win by repeating this process.
17+
*
18+
* So, the solution simply checks whether there is at least one vowel in the string.
19+
*
20+
* Time Complexity: O(n), where n is the length of the string.
21+
* Space Complexity: O(1), constant space used.
22+
*/
23+
public class Solution {
24+
public boolean doesAliceWin(String s) {
25+
for (int i = 0; i < s.length(); i++) {
26+
char ch = s.charAt(i);
27+
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
28+
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
29+
return true;
30+
}
31+
}
32+
return false;
33+
}
34+
}

0 commit comments

Comments
 (0)