LeetCode in Kotlin

3174. Clear Digits

Easy

You are given a string s.

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

Return the resulting string after removing all digits.

Example 1:

Input: s = “abc”

Output: “abc”

Explanation:

There is no digit in the string.

Example 2:

Input: s = “cb34”

Output: “”

Explanation:

First, we apply the operation on s[2], and s becomes "c4".

Then we apply the operation on s[1], and s becomes "".

Constraints:

Solution

class Solution {
    fun clearDigits(s: String): String {
        val result = StringBuilder()
        for (ch in s.toCharArray()) {
            if (ch in '0'..'9') {
                if (result.isNotEmpty()) {
                    result.deleteCharAt(result.length - 1)
                }
            } else {
                result.append(ch)
            }
        }
        return result.toString()
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy