You are given a string dominoes
representing the initial state of dominos, where:
dominoes[i] = 'L'
represents a domino that has been pushed to the left,dominoes[i] = 'R'
represents a domino that has been pushed to the right, anddominoes[i] = '.'
represents a domino that is standing still.Return a string representing the final state.
dominoes
can be up to 100,000.left
and right
) to find segments of dominos influenced by ‘R’ and ‘L’.def pushDominos(dominoes: str) -> str:
n = len(dominoes)
symbols = list(dominoes)
forces = [0] * n
# Scan left-to-right for 'R' forces
force = 0
for i in range(n):
if symbols[i] == 'R':
force = n
elif symbols[i] == 'L':
force = 0
else:
force = max(force - 1, 0)
forces[i] += force
# Scan right-to-left for 'L' forces
force = 0
for i in range(n-1, -1, -1):
if symbols[i] == 'L':
force = n
elif symbols[i] == 'R':
force = 0
else:
force = max(force - 1, 0)
forces[i] -= force
# Determine the final state
result = []
for f in forces:
if f > 0:
result.append('R')
elif f < 0:
result.append('L')
else:
result.append('.')
return "".join(result)
# Example usage
dominoes = "RR.L"
print(pushDominos(dominoes)) # Output: "RR.L"
This solution ensures all dominos are properly accounted for within linear scanning, making it efficient for the input constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?