Skip to content

Latest commit

 

History

History
61 lines (44 loc) · 1.51 KB

_2211. Count Collisions on a Road.md

File metadata and controls

61 lines (44 loc) · 1.51 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 14, 2024

Last updated : July 14, 2024


Related Topics : String, Stack, Simulation

Acceptance Rate : 43.96 %


Solutions

Python

class Solution:
    def countCollisions(self, directions: str) -> int:
        cols = 0
        lCounter = 0
        last = 'R'

        for c in reversed(directions) :
            if c == 'L' :
                lCounter += 1
                last = c

            elif c == 'S' :
                cols += lCounter
                lCounter = 0
                last = c

            elif c == 'R' and last != 'R' : # R
                cols += lCounter + 1
                lCounter = 0
                last = 'S'

        return cols
class Solution:
    def countCollisions(self, directions: str) -> int:
        directions = directions.lstrip('L').rstrip('R')
        return len(directions) - directions.count('S')