From 8a3c86454cc10a58720ec51ff606fbd2820e8169 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Thu, 29 Dec 2022 12:39:25 -0800 Subject: [PATCH 1/2] solve 293. Flip Game --- 293.flip-game.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 293.flip-game.js diff --git a/293.flip-game.js b/293.flip-game.js new file mode 100644 index 00000000..42357b18 --- /dev/null +++ b/293.flip-game.js @@ -0,0 +1,25 @@ +/* URL of this problem + * https://leetcode.com/problems/flip-game/description/ + * + * @param {string} currentState + * @return {string[]} + */ + +var generatePossibleNextMoves = function(currentState) { + const PossibleStates = []; + + for (let i = 0; i < currentState.length - 1; i++) { + const CurrentStateArr = [...currentState]; + + if (CurrentStateArr[i] === "+" && CurrentStateArr[i + 1] === "+") { + CurrentStateArr[i] = "-"; + CurrentStateArr[i + 1] = "-"; + + PossibleStates.push(CurrentStateArr.join("")); + } + } + + return PossibleStates; +}; + +module.exports = generatePossibleNextMoves; \ No newline at end of file From 26de1ff76ffd8b2810be9f76aa165acdc12f29fe Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Thu, 29 Dec 2022 12:39:53 -0800 Subject: [PATCH 2/2] test function generatePossibleNextMoves --- generatePossibleNextMoves.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 generatePossibleNextMoves.test.js diff --git a/generatePossibleNextMoves.test.js b/generatePossibleNextMoves.test.js new file mode 100644 index 00000000..ca0d0ce2 --- /dev/null +++ b/generatePossibleNextMoves.test.js @@ -0,0 +1,17 @@ +const generatePossibleNextMoves = require("./293.flip-game"); + +test("Return an array of all the possible states after one valid move", () => { + expect(generatePossibleNextMoves("++++")).toEqual(["--++","+--+","++--"]); +}); + +test("Return an empty array if the input currentState is made of only one of '+'", () => { + expect(generatePossibleNextMoves("+")).toEqual([]); +}); + +test("Return an empty array if the input currentState is an empty string", () => { + expect(generatePossibleNextMoves("")).toEqual([]); +}); + +test("Return an empty array if the input currentState comprises only '-", () => { + expect(generatePossibleNextMoves("----")).toEqual([]); +}); \ No newline at end of file