Back to all solutions

#3174 - Clear Digits

Problem Description

You are given a string s.

Your task is to remove all digits by doing this operation repeatedly:

  • Delete the first digit and the closest non-digit character to its left.

Return the resulting string after removing all digits.

Solution

/**
 * @param {string} s
 * @return {string}
 */
var clearDigits = function(s) {
  const stack = [];

  for (const character of s) {
    if (!isNaN(character) && stack.length) {
      stack.pop();
    } else {
      stack.push(character);
    }
  }

  return stack.join('');
};