Skip to content

Commit 218a9b7

Browse files
committed
Add solution #293
1 parent da456a3 commit 218a9b7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
290|[Word Pattern](./solutions/0290-word-pattern.js)|Easy|
282282
291|[Word Pattern II](./solutions/0291-word-pattern-ii.js)|Medium|
283283
292|[Nim Game](./solutions/0292-nim-game.js)|Easy|
284+
293|[Flip Game](./solutions/0293-flip-game.js)|Easy|
284285
295|[Find Median from Data Stream](./solutions/0295-find-median-from-data-stream.js)|Hard|
285286
297|[Serialize and Deserialize Binary Tree](./solutions/0297-serialize-and-deserialize-binary-tree.js)|Hard|
286287
299|[Bulls and Cows](./solutions/0299-bulls-and-cows.js)|Medium|

solutions/0293-flip-game.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 293. Flip Game
3+
* https://leetcode.com/problems/flip-game/
4+
* Difficulty: Easy
5+
*
6+
* You are playing a Flip Game with your friend.
7+
*
8+
* You are given a string currentState that contains only '+' and '-'. You and your friend take
9+
* turns to flip two consecutive "++" into "--". The game ends when a person can no longer make
10+
* a move, and therefore the other person will be the winner.
11+
*
12+
* Return all possible states of the string currentState after one valid move. You may return
13+
* the answer in any order. If there is no valid move, return an empty list [].
14+
*/
15+
16+
/**
17+
* @param {string} currentState
18+
* @return {string[]}
19+
*/
20+
var generatePossibleNextMoves = function(currentState) {
21+
const results = [];
22+
23+
for (let i = 0; i < currentState.length - 1; i++) {
24+
if (currentState[i] === '+' && currentState[i + 1] === '+') {
25+
results.push(
26+
currentState.slice(0, i) + '--' + currentState.slice(i + 2)
27+
);
28+
}
29+
}
30+
31+
return results;
32+
};

0 commit comments

Comments
 (0)