-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (68 loc) · 2.69 KB
/
main.cpp
File metadata and controls
72 lines (68 loc) · 2.69 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Source: https://leetcode.com/problems/vowels-game-in-a-string
// Title: Vowels Game in a String
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Alice and Bob are playing a game on a string.
//
// You are given a string `s`, Alice and Bob will take turns playing the following game where Alice starts **first**:
//
// - On Alice's turn, she has to remove any **non-empty substring** from `s` that contains an **odd** number of vowels.
// - On Bob's turn, he has to remove any **non-empty substring** from `s` that contains an **even** number of vowels.
//
// A **substring** is a contiguous sequence of characters within 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 `true` if Alice wins the game, and `false` otherwise.
//
// The English vowels are: `a`, `e`, `i`, `o`, and `u`.
//
// **Example 1:**
//
// ```
// Input: s = "leetcoder"
// Output: true
// Explanation:
// Alice can win the game as follows:
// - Alice plays first, she can delete the underlined substring in `s = "**leetco**der"` which contains 3 vowels. The resulting string is `s = "der"`.
// - Bob plays second, he can delete the underlined substring in `s = "**d**er"` which contains 0 vowels. The resulting string is `s = "er"`.
// - Alice plays third, she can delete the whole string `s = "**er**"` which contains 1 vowel.
// - Bob plays fourth, since the string is empty, there is no valid play for Bob. So Alice wins the game.
// ```
//
// **Example 2:**
//
// ```
// Input: s = "bbcd"
// Output: false
// Explanation:
// There is no valid play for Alice in her first turn, so Alice loses the game.
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^5`
// - `s` consists only of lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
using namespace std;
// Math
//
// If there is no vowel, then Alice loses since she can't move.
//
// If there are odd vowels, then Alice wins.
// She can remove the whole string, and then Bob can't move.
//
// If there are even vowels, then Alice wins.
// After she removing any substring, there will be odd vowels.
// No matter what Bob does, there will still be odd vowels.
// Alice can remove the whole string, and then Bob can't move.
class Solution {
public:
bool doesAliceWin(string s) {
auto check = [](char c) -> bool { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; };
return any_of(s.cbegin(), s.cend(), check);
}
};