-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathflipGame.js
More file actions
32 lines (25 loc) · 745 Bytes
/
flipGame.js
File metadata and controls
32 lines (25 loc) · 745 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
26
27
28
29
30
31
32
/**
* @param {string} currentState
* @return {string[]}
*/
var generatePossibleNextMoves = function (currentState) {
const result = [];
let buffer = [];
for (let i = 0; i < currentState.length - 1; i++) {
if (currentState[i] === '+' && currentState[i + 1] === '+') {
for (let j = 0; j < i; j++) {
buffer.push(currentState[j]);
}
buffer.push('--');
for (let k = i + 2; k < currentState.length; k++) {
buffer.push(currentState[k]);
}
}
if (buffer.length > 0) {
result.push(buffer.join(''));
}
buffer = [];
}
return result;
};
console.log(generatePossibleNextMoves("++++"));