Cover image for LeetCode Daily Problem P3227 String Vowel Game

LeetCode Daily Problem P3227 String Vowel Game


Timeline

Timeline

2025-09-12

init

State Analysis

Problem:

This problem can be viewed by drawing a state machine

image-20250912110341407
image-20250912110341407

Xiaohong goes first. If the total number of vowels in the entire string is odd, then she takes all the substrings directly and wins.

Xiaohong goes first. If the total number of vowels in the entire string is even, unless it is 0, Xiaohong loses if she goes first; if it is non-zero, then after taking one substring with an odd number of vowels, at least 2-1=1 vowel substring remains. At this point, Xiaoming must take an even number, so he can only take 0. Therefore, it must enter the third state of the state machine, where the entire string has an odd number of vowels left, and it is Xiaohong’s turn to take an odd number. She takes all substrings directly and wins.

Therefore, as long as this string has any vowel, Xiaohong wins. Otherwise,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdbool.h>
#include <string.h>
bool isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' ||
c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return true;
}
return false;
}

bool doesAliceWin(char *s) {
int vowel_num = 0;
int len;
len = strlen(s);
// Count the total number of vowels
for (int i = 0; i < len; i++) {
if (isVowel(s[i])) {
vowel_num++;
}
}
if (vowel_num == 0) {
return false;
}

if (vowel_num % 2 == 0) { // Even
return true;
} else { // Odd
return true;
}
}