Removing Stars From a String

MediumStringStackSimulation

Solution

export function removeStars(s: string): string {
  const stack: string[] = [];
  for (const char of s) {
    if (char === '*') {
      stack.pop();
    } else {
      stack.push(char);
    }
  }
  return stack.join('');
}

Complexity

  • Time: O(N)
  • Space: O(N)