LeetCode 293: Flip Game
Problem Description
Explanation
This problem asks to determine all possible states after flipping a "++" to "--" in a given string. We can solve this by iterating through the string and checking each occurrence of "++". If found, we can flip it to "--" and add the resulting string to our list of solutions.
- Initialize an empty list to store solutions.
- Iterate through the string, checking for occurrences of "++".
- If found, create a new string by flipping the "++" to "--" and add it to the list of solutions.
- Return the list of solutions.
Time Complexity: O(n) where n is the length of the input string. Space Complexity: O(n) considering the space required to store the resulting solutions.
Solutions
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<String> generatePossibleNextMoves(String s) {
List<String> result = new ArrayList<>();
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '+' && s.charAt(i + 1) == '+') {
StringBuilder sb = new StringBuilder(s);
sb.setCharAt(i, '-');
sb.setCharAt(i + 1, '-');
result.add(sb.toString());
}
}
return result;
}
}
Related LeetCode Problems
Loading editor...