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; + } +};