LeetCode Entry

2390. Removing Stars From a String

11.04.2023 medium 2023 kotlin

we can use a Stack, or just StringBuilder

2390. Removing Stars From a String medium


fun removeStars(s: String): String = StringBuilder().apply {
    s.forEach {
        if (it == '*') setLength(length - 1)
        else append(it)
    }
}.toString()

blog post

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/177

Intuition

Iterate over a string. When * symbol met, remove last character, otherwise add it.

Approach

  • we can use a Stack, or just StringBuilder

    Complexity

  • Time complexity:
\[O(n)\]
  • Space complexity:
\[O(n)\]