-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflip_game.cpp
More file actions
40 lines (34 loc) · 1.06 KB
/
flip_game.cpp
File metadata and controls
40 lines (34 loc) · 1.06 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
/*
Problem: The Flip Game
(https://leetcode.com/problems/flip-game/)
Information given:
A string s of + or -, e.g. s = "++--+-+-"
You can flip any consecutive "++" into "--"
Possible states of s after one flip?
Information derived:
Seemingly random, no patterns. Hard to extract extra info.
Have to look at every character or else no way of knowing - at least O(n) time
Relationship: If at any "+", the character before it is "+" then, we know it's a possible flip.
-> single pass through string and check whether or not there are consecutive "+"'s...
if there are, add it to the vector of possible states
*/
#include <vector>
#include <string>
#include <iostream>
using namespace std;
vector<string> generatePossibleNextMoves(string s) {
vector<string> possibleStates;
if (s.size() == 0 || s.size() == 1) return possibleStates;
for (int i = 1; i < s.size(); i++) {
if (s[i-1] == '+' && s[i] == '+') {
s[i-1] = '-';
s[i] = '-';
possibleStates.push_back(s);
s[i-1] = '+';
s[i] = '+';
}
}
return possibleStates;
}
int main() {
}