> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Removing Stars From a String

Leetcode Problem 2390: Removing Stars From a String

Problem Summary

Given a string s containing letters and '*' characters, repeatedly remove the nearest non-star character to the left of each '*' along with the '*' itself. Return the resulting string after all stars are processed. The input is guaranteed to produce a valid result.

Constraints:

  • The string has between 1 and 100,000 characters.
  • It consists of lowercase English letters and '*'.
  • There are always enough non-star characters to the left for every '*'.

Techniques

  • String
  • Stack
  • Simulation

Solution

function removeStars(s: string): string {
    let stack = []

    for (let i = 0; i < s.length; i++) {
        let currentChar = s.charAt(i)
        
        if (currentChar === "*") {
            stack.pop()
        } else {
            stack.push(currentChar)
        }
    }

    return stack.join("")
};

console.log(removeStars("leet**cod*e"))