Count Collisions on a Road

medium stack simulation string

Problem

Cars sit on a road, numbered left to right. A string directions tells whether each car moves Left (L), Right (R), or Stays (S). All moving cars share one speed. A head-on R/L crash adds 2 collisions; a moving car hitting a stopped car adds 1. Collided cars stop forever. Return the total number of collisions.

Inputdirections = "RLRSLL"
Output5
R/L head-on (+2), then three more cars each pile into the stopped cluster (+1 each) for a total of 5.
Inputdirections = "LLRR"
Output0
The L's drive off to the left and the R's drive off to the right, so nothing ever meets.

def count_collisions(directions):
    stack = []
    collisions = 0
    for ch in directions:
        if ch == "R":
            stack.append("R")
        elif ch == "S":
            while stack and stack[-1] == "R":
                collisions += 1
                stack.pop()
            stack.append("S")
        else:
            if not stack or stack[-1] == "L":
                stack.append("L")
            else:
                collisions += 1
                if stack[-1] == "R":
                    collisions += 1
                stack.pop()
                while stack and stack[-1] == "R":
                    collisions += 1
                    stack.pop()
                stack.append("S")
    return collisions
function countCollisions(directions) {
  const stack = [];
  let collisions = 0;
  for (const ch of directions) {
    if (ch === "R") {
      stack.push("R");
    } else if (ch === "S") {
      while (stack.length && stack[stack.length - 1] === "R") {
        collisions++;
        stack.pop();
      }
      stack.push("S");
    } else {
      if (!stack.length || stack[stack.length - 1] === "L") {
        stack.push("L");
      } else {
        collisions++;
        if (stack[stack.length - 1] === "R") collisions++;
        stack.pop();
        while (stack.length && stack[stack.length - 1] === "R") {
          collisions++;
          stack.pop();
        }
        stack.push("S");
      }
    }
  }
  return collisions;
}
int countCollisions(String directions) {
    Deque<Character> stack = new ArrayDeque<>();
    int collisions = 0;
    for (char ch : directions.toCharArray()) {
        if (ch == 'R') {
            stack.push('R');
        } else if (ch == 'S') {
            while (!stack.isEmpty() && stack.peek() == 'R') {
                collisions++;
                stack.pop();
            }
            stack.push('S');
        } else {
            if (stack.isEmpty() || stack.peek() == 'L') {
                stack.push('L');
            } else {
                collisions++;
                if (stack.peek() == 'R') collisions++;
                stack.pop();
                while (!stack.isEmpty() && stack.peek() == 'R') {
                    collisions++;
                    stack.pop();
                }
                stack.push('S');
            }
        }
    }
    return collisions;
}
int countCollisions(string directions) {
    vector<char> stk;
    int collisions = 0;
    for (char ch : directions) {
        if (ch == 'R') {
            stk.push_back('R');
        } else if (ch == 'S') {
            while (!stk.empty() && stk.back() == 'R') {
                collisions++;
                stk.pop_back();
            }
            stk.push_back('S');
        } else {
            if (stk.empty() || stk.back() == 'L') {
                stk.push_back('L');
            } else {
                collisions++;
                if (stk.back() == 'R') collisions++;
                stk.pop_back();
                while (!stk.empty() && stk.back() == 'R') {
                    collisions++;
                    stk.pop_back();
                }
                stk.push_back('S');
            }
        }
    }
    return collisions;
}
Time: O(n) Space: O(n)