Skip to content

Commit f5726c1

Browse files
solves #293: flip game in java
1 parent ac58f56 commit f5726c1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
| 290 | [Word Pattern](https://leetcode.com/problems/word-pattern) | [![Java](assets/java.png)](src/WordPattern.java) [![Python](assets/python.png)](python/word_pattern.py) | |
257257
| 291 | 🔒 [Word Pattern II](https://leetcode.com/problems/word-pattern-ii) | | |
258258
| 292 | [Nim Game](https://leetcode.com/problems/nim-game) | [![Java](assets/java.png)](src/NimGame.java) [![Python](assets/python.png)](python/nim_game.py) | |
259-
| 293 | 🔒 [Flip Game](https://leetcode.com/problems/flip-game) | | |
259+
| 293 | 🔒 [Flip Game](https://leetcode.com/problems/flip-game) | [![Java](assets/java.png)](src/FlipGame.java) | |
260260
| 294 | 🔒 [Flip Game II](https://leetcode.com/problems/flip-game-ii) | | |
261261
| 298 | 🔒 [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence) | | |
262262
| 299 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows) | [![Java](assets/java.png)](src/BullsAndCows.java) [![Python](assets/python.png)](python/bulls_and_cows.py) | |

src/FlipGame.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/flip-game
2+
// N = |currentState|
3+
// T: O(N^2)
4+
// S: O(N^2)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class FlipGame {
10+
public List<String> generatePossibleNextMoves(String currentState) {
11+
final List<String> result = new ArrayList<>();
12+
for (int i = 0 ; i < currentState.length() - 1 ; i++) {
13+
if (currentState.charAt(i) == '+' && currentState.charAt(i + 1) == '+') {
14+
result.add(flip(currentState, i));
15+
}
16+
}
17+
return result;
18+
}
19+
20+
private static String flip(String state, int index) {
21+
return state.substring(0, index) + "--" + state.substring(index + 2);
22+
}
23+
}

0 commit comments

Comments
 (0)