Skip to content
LC-2211 Medium LeetCode

2211. Count Collisions on a Road

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 44% Topics: String, Stack, Simulation
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# counting, simulation
class Solution(object):
    def countCollisions(self, directions):
        """
        :type directions: str
        :rtype: int
        """
        result = cnt = 0
        smooth = 1
        for x in directions:
            if x == 'R':
                cnt += 1
            elif x == 'S' or (cnt or not smooth):
                result += cnt+int(x == 'L')
                cnt = smooth = 0
        return result

Solution from kamyu104/LeetCode-Solutions · MIT