Similar Problems

Similar Problems not available

Removing Stars From A String - Leetcode Solution

Companies:

LeetCode:  Removing Stars From A String Leetcode Solution

Difficulty: Medium

Topics: string stack simulation  

Problem Statement:

Given a string str that contains some number of asterisks (*), replace every adjacent pair of asterisks with just one asterisk and repeat this process until the string no longer contains pairs of adjacent asterisks.

Solution:

Approach: We can create a new string and go through the given string character by character. At each step, we check if the current character is an asterisk and if it is followed by another asterisk. If this is the case, we ignore the current and next asterisk and move to the next character. Otherwise, we add the current character to the new string. We repeat this process until there are no more pairs of adjacent asterisks.

Algorithm:

  1. Initialize a variable newStr as an empty string.
  2. Initialize a flag hasPair to true.
  3. While hasPair is true: a. Set hasPair to false. b. Loop through the characters of the given string str: i. If the current character is an asterisk and the next character is also an asterisk, set hasPair to true and move to the next character. ii. Otherwise, add the current character to the new string newStr.
  4. Assign the value of newStr to the given string str.

Code:

Here's the Python code implementing the above algorithm:

class Solution:
    def removeDuplicates(self, str: str) -> str:
        newStr = ""
        hasPair = True
        while hasPair:
            hasPair = False
            i = 0
            while i < len(str):
                if str[i] == "*" and i+1 < len(str) and str[i+1] == "*":
                    hasPair = True
                    i += 2
                else:
                    newStr += str[i]
                    i += 1
            str = newStr
            newStr = ""
        return str

The time complexity of this algorithm is O(n^2) in the worst case, where n is the length of the given string. This is because, in the worst case, we need to traverse the entire string multiple times to remove all pairs of adjacent asterisks until no more pairs remain. However, in practice, the running time will be much lower as the string is unlikely to have many pairs of adjacent asterisks.

Example:

Let's take the input string as "a*bcd".

Initially, the new string is empty, and hasPair is true.

In the first iteration:

hasPair is set to false.

i = 0, str[i] is "a", which is added to the new string.

i = 1, str[i] is "*", and i+1 < len(str), so we have a pair of adjacent asterisks. hasPair is set to true, and i is incremented by 2.

i = 3, str[i] is "*", and i+1 < len(str), so we have another pair of adjacent asterisks. hasPair is still set to true, and i is incremented by 2.

i = 5, str[i] is "b", which is added to the new string.

i = 6, str[i] is "*", and i+1 < len(str), but we have already removed all pairs of adjacent asterisks, so we add the current asterisk to the new string.

i = 7, str[i] is "c", which is added to the new string.

i = 8, str[i] is "*", and i+1 < len(str), so we have another pair of adjacent asterisks. hasPair is set to true, and i is incremented by 2.

i = 10, str[i] is "d", which is added to the new string.

Now, the new string is "abcd", and hasPair is false.

Since hasPair is false, we return the new string "abcd".

Therefore, the output is "abcd".

Removing Stars From A String Solution Code

1