Cover image for LeetCode Daily Problem P3227 String Vowel Game

LeetCode Daily Problem P3227 String Vowel Game

Words 311
Views
Visitors

Timeline

Timeline

2025-09-12

init

State analysis

Problem:

This problem can be analyzed by drawing a state machine.

State machine diagram for the String Vowel Game
State machine diagram for the String Vowel Game

Xiaohong moves first. If the total number of vowels in the whole string is odd, she can take the entire string directly and win.

Xiaohong moves first. If the total number of vowels in the whole string is even, she loses as the first player only when it is 0; if it is non-zero, then after taking a substring with an odd number of vowels, at least 2-1=1 vowels remain. At this time, Xiaoming has to take an even number, so he can only take 0. Therefore, it is sure to enter the third state of the state machine, where the whole string has an odd number of vowels left. Then it is Xiaohong’s turn to take an odd number, so she directly takes all the strings and wins.

Therefore, as long as this string has vowels, Xiaohong wins; otherwise

12345678910111213141516171819202122232425262728293031
#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;  }}
Loading comments…