2
cars on left side which are moving in left direction are never going to collide,3
Similarly, cars on right side which are moving right side are never going to collide.5
In between them every car is going to collide.9
public int countCollisions(String directions) {10
int left = 0, right = directions.length() - 1;12
while (left < directions.length() && directions.charAt(left) == 'L') {16
while (right >= 0 && directions.charAt(right) == 'R') {21
for (int i = left; i <= right; i++) {22
if (directions.charAt(i) != 'S') {26
// combining these three loops - TC : O(N).