Skip to main content
Back to problems
#293
Easy Algorithms

Flip game

String
65.0% acceptance
Mar 31, 2026
232
477
You are playing a Flip Game with your friend. You are given a string currentState that contains only '+' and '-'. You and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move, and therefore the other person will be the winner. Return all possible states of the string currentState after one valid move. You may return the answer in any order. If there is no valid move, return an empty list [].

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn generate_possible_next_moves(current_state: String) -> Vec<String> {
    let bytes = current_state.as_bytes();
    let mut result = Vec::new();
    for i in 0..bytes.len().saturating_sub(1) {
      if bytes[i] == b'+' && bytes[i + 1] == b'+' {
        let mut s = current_state.clone().into_bytes();
        s[i] = b'-';
        s[i + 1] = b'-';
        result.push(unsafe { String::from_utf8_unchecked(s) });
      }
    }
    result
  }
}