-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0293-flip-game.js
More file actions
25 lines (22 loc) · 782 Bytes
/
0293-flip-game.js
File metadata and controls
25 lines (22 loc) · 782 Bytes
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
/**
* Flip Game
* Time Complexity: O(N^2)
* Space Complexity: O(N^2)
*/
var generatePossibleNextMoves = function (currentState) {
const possibleNewStates = [];
let iterateIndex = 0;
const stateLength = currentState.length;
while (iterateIndex < stateLength - 1) {
const charOne = currentState[iterateIndex];
const charTwo = currentState[iterateIndex + 1];
if (charOne === '+' && charTwo === '+') {
const startSegment = currentState.slice(0, iterateIndex);
const endSegment = currentState.slice(iterateIndex + 2);
const nextConfiguration = startSegment + '--' + endSegment;
possibleNewStates.push(nextConfiguration);
}
iterateIndex++;
}
return possibleNewStates;
};