Skip to content

Practice Count Collisions on a Road

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2211
LeetCode Problem

Count Collisions on a Road

Problem Statement

directions lists cars from left to right at distinct positions: L moves left, R moves right, and S stays still. All moving cars have the same speed. Opposing moving cars colliding add 2 to the collision count; a moving car hitting a stationary car adds 1. Cars involved become stationary. Return the total collision count.

Source: Count Collisions on a Road on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
directions = "RLRSLL"
Output
5

# Example 2

Input
directions = "LLRR"
Output
0

Constraints

1 <= directions.length <= 10^5
directions[i] is either 'L', 'R', or 'S'.

Related LeetCode Problems

Practice more problems similar to Count Collisions on a Road.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.