-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflip_game.py
More file actions
31 lines (25 loc) · 1.03 KB
/
flip_game.py
File metadata and controls
31 lines (25 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# https://www.lintcode.com/problem/flip-game/description
# You are playing the following Flip Game with your friend: Given a string that contains only these two characters:
# + 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.
#
# Write a function to compute all possible states of the string after one valid move.
"""
>>> Solution().generatePossibleNextMoves('++++')
['--++', '+--+', '++--']
>>> Solution().generatePossibleNextMoves('---+++-+++-+')
['-----+-+++-+', '---+---+++-+', '---+++---+-+', '---+++-+---+']
"""
class Solution:
"""
@param s: the given string
@return: all the possible states of the string after one valid move
"""
def generatePossibleNextMoves(self, s):
result = []
i = 0
while i + 1 < len(s):
if s[i] == '+' and s[i + 1] == '+':
result.append(s[:i] + '-' + '-' + s[i + 2:])
i += 1
return result