Ctrl K

Crashes on One Lane

Medium
Cars stand on an endless straight road, numbered from 0 left to right, each at its own point. The string moves describes them: moves[i] is 'L' (the car drives left), 'R' (drives right) or 'S' (stays put). All moving cars travel at the same speed. Crashes are scored like this: - two cars driving toward each other collide, adding 2; - a moving car hits a standing one, adding 1. After a crash, the cars involved stop where they collided and stay there. Nobody else ever changes direction. Return the total crash score.
Example 1
moves="RSLR" → 2

Car 0 runs into the parked car 1 (+1), and car 2 hits it from the other side (+1). Car 3 drives off to the right. Total 2.

Example 2
moves="RRL" → 3

Cars 1 and 2 meet head-on (+2) and stop, then car 0 runs into them (+1). Total 3.

Example 3
moves="LSR" → 0
Constraints
  • 1 ≤ len(moves) ≤ 10⁵
  • every character of moves is 'L', 'R' or 'S'
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsErase the Pattern Until It Is Gone MediumStar as Backspace MediumMirror Letter Pairing Score MediumSame Text After Erasing EasyCollapse Letter Pairs Easy
loading Python…
Input
moves="RSLR"
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 180 tests on the server, hidden ones included.