634. Count Collisions on a Road

MediumStackStackSimulationString

Cars on a road are given by a string of 'L' (moving left), 'R' (moving right), and 'S' (stationary). A moving car hitting a stationary car or two cars colliding produces collisions: a car hitting a stationary car counts as 1 collision; two moving cars colliding counts as 2. Cars that eventually stop stay put. Return the total number of collisions. The input is JSON {directions}.

Input: JSON {directions}.

Output: Integer — the total number of collisions.

Examples

Example 1
Input: {"directions":"RLRSLL"}
Output: 5
Explanation: Multiple collisions occur in the middle.
Example 2
Input: {"directions":"LLRR"}
Output: 0
Explanation: Cars move apart; no collisions.
Example 3
Input: {"directions":"RRRLLL"}
Output: 6
Explanation: All six cars collide.

Constraints

Asked by

GoogleAmazon
Solve this problem in the editor →