Knowee
Questions
Features
Study Tools

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. 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:1 <= s.length <= 100s consists only of lowercase English letters and digits.The input is generated such that it is possible to delete all digits.

Question

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. 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:1 <= s.length <= 100s consists only of lowercase English letters and digits.The input is generated such that it is possible to delete all digits.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, you can follow these steps:

  1. Initialize an empty stack.
  2. Iterate over the characters in the string from left to right.
  3. If the current character is a non-digit, push it into the stack.
  4. If the current character is a digit, pop the top element from the stack.
  5. After the iteration, the stack contains the characters of the resulting string. Convert the stack into a string and return it.

Here is a Python solution following the above steps:

def removeDigits(s):
    stack = []
    for ch in s:
        if ch.isdigit():
            if stack:
                stack.pop()
        else:
            stack.append(ch)

This problem has been solved

Similar Questions

Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming

Write a program to remove all digit in given string.Below given is the program to remove all punctuation's in a string and print the result.import stringpunctuations = string.punctuationresult = " "str = "List - []\n tuple - ()\n Dictionary - {}\n Comment - #\n Multiply - *\n not - !\n and - &\n or - |\n format specifier - %\n String - " " $ @ ; : ' / + = " for i in str: if i not in punctuations: result = result + iprint("String after removing all Punctuation's:", result)Here,We are importing string module to know the list of punctuation's using string.punctuation. Then we compare punctuations in the given string with the string.punctuation module and remove them from the input string and print the resultant string. str variable contains input string. for clause to iterate over the list of characters in the input string. if condition to check if a punctuation of the input string exists in punctuation variable or not.Output:Set of punctuations in string.punctuation is: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~String after removing all Punctuation's is: List tuple Dictionary Comment Multiply not and or format specifier String Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming

Write a program for reading a position and deleting a character at that specified location in a given string

Given a string of characters, find the first repeating character.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains a single string of characters.Output FormatFor each test case, print the first repeating character, separated by a new line. If there are none, print '.'.Constraints1 <= T <= 1000'a' <= str[i] <= 'z'1 <= len(str) <= 104ExampleInput4datastructuresalgorithmssmartinterviewshackerrankOutputa.sa

Given a string s containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s with the same parity at most once.Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not. Example 1:Input: s = "45320"Output: "43520"Explanation:

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.