From ee40352dc9706c273bac1f7b73ef955d5fbaeaa1 Mon Sep 17 00:00:00 2001 From: chayan das Date: Thu, 4 Dec 2025 23:11:11 +0530 Subject: [PATCH] Create 2211. Count Collisions on a Road --- 2211. Count Collisions on a Road | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2211. Count Collisions on a Road diff --git a/2211. Count Collisions on a Road b/2211. Count Collisions on a Road new file mode 100644 index 0000000..a0bbff7 --- /dev/null +++ b/2211. Count Collisions on a Road @@ -0,0 +1,23 @@ +class Solution { +public: + int countCollisions(string directions) { + int n = directions.size(); + int left = 0, right = n - 1; + int cnt = 0; + while (left < n && directions[left] == 'L'){ + left++; + } + while (right >= 0 && directions[right] == 'R'){ + right--; + } + if (left > right){ + return 0; + } + for (int i = left; i <= right; i++){ + if (directions[i] != 'S'){ + cnt++; + } + } + return cnt; + } +};