1. Built-In Function

class Solution:
    def generatePossibleNextMoves(self, currentState: str) -> List[str]:
        res = []
        i = currentState.find("++")
        while i != -1:
            res.append(currentState[:i] + "--" + currentState[i + 2:])
            i = currentState.find("++", i + 1)
        return res

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity:
    • O(1)O(1) extra space.
    • O(n2)O(n ^ 2) space for the output list.

Where nn is the length of the string currentStatecurrentState.


2. Iteration

class Solution:
    def generatePossibleNextMoves(self, currentState: str) -> List[str]:
        res = []
        for i in range(len(currentState) - 1):
            if currentState[i] == "+" and currentState[i + 1] == "+":
                res.append(currentState[:i] + "--" + currentState[i + 2:])
        return res

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity:
    • O(1)O(1) extra space.
    • O(n2)O(n ^ 2) space for the output list.

Where nn is the length of the string currentStatecurrentState.


3. Simulation

class Solution:
    def generatePossibleNextMoves(self, currentState: str) -> List[str]:
        chars = list(currentState)
        res = []
        for i in range(len(chars) - 1):
            if chars[i] == "+" and chars[i + 1] == "+":
                chars[i] = chars[i + 1] = "-"
                res.append("".join(chars))
                chars[i] = chars[i + 1] = "+"
        return res

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity:
    • O(n)O(n) extra space for the character array.
    • O(n2)O(n ^ 2) space for the output list.

Where nn is the length of the string currentStatecurrentState.