-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathflip_game_2.cpp
30 lines (27 loc) · 879 Bytes
/
flip_game_2.cpp
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
#include<bits/stdc++.h>
using namespace std;
// problem : https://www.lintcode.com/problem/flip-game-ii/
// very good question
class Solution {
public:
bool canWin(string &s) {
// write your code here
if(s.length()<2) return false;
return flipGame(s);
}
bool flipGame(string &s){
for(int i=0;i<s.length()-1;i++){
if(s[i]=='+' && s[i+1]=='+'){
// find the next state of the string and see if the other player can win
// with the next state
string nextState = s.substr(0,i)+"--"+s.substr(i+2);
// if the player can't win with the next state, i will win the flipGame
// and hence return true
if(!flipGame(nextState)){
return true;
}
}
}
return false;
}
};