2317. Count Collisions on a Road
My accepted Python solution to LeetCode problem 2317, Count Collisions on a Road, running in 54ms.
- Difficulty: Medium
- Python
- Runtime 54ms
- Memory 17.9MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 54ms, memory 17.9MB, accepted 2025-12-30.
class Solution:
def countCollisions(self, directions: str) -> int:
# Cars going left at the start never collide
# Cars going right at the end never collide
# All other moving cars will eventually collide
# Strip L's from left and R's from right
directions = directions.lstrip('L').rstrip('R')
# Count remaining moving cars (L's and R's)
collisions = 0
for c in directions:
if c != 'S':
collisions += 1
return collisions