Skip to main content

Lexicographically Minimum String After Removing Stars

Problem Description​

You are given a string s. It may contain any number of '' characters. Your task is to remove all '' characters.

While there is a '*', do the following operation:

Delete the leftmost '' and the smallest non-'' character to its left. If there are several smallest characters, you can delete any of them. Return the lexicographically smallest resulting string after removing all '*' characters.

Examples​

Example 1:

Input: s = "aaba*"

Output: "aab"

Explanation:

We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest.

Example 2:

Input: s = "abc"

Output: "abc"

Explanation:

There is no '*' in the string.

Constraints​

  • 1 <= s.length <= 10^5
  • s consists only of lowercase English letters and '*'.
  • The input is generated such that it is possible to delete all '*' characters.

Solution for Lexicographically Minimum String After Removing Stars Problem​

Approach​

Initialization:​

  • We initialize two sets:
  • st: A set to store pairs of (character value, negative index). This helps us quickly identify and remove the leftmost character when we encounter a star.
  • del: A set to keep track of the indices of characters that need to be deleted (either because they are stars or they were removed by stars).

Traversing the String:​

  • We iterate over the string from left to right.

For each character:​

  • If the character is a star (*):
  • We identify the leftmost character in the set st by taking the smallest element (due to the ordering by character value and then by index).
  • We remove this leftmost character from the set st.
  • We record the indices of this leftmost character and the star itself in the del set.
  • If the character is not a star:
  • We add a pair of (character value, negative index) to the set st.

Building the Result:​

  • We initialize an empty string ans to store the result.
  • We iterate over the original string again.
  • For each character:
  • If the index of the character is not in the del set (i.e., it hasn't been marked for deletion), we append it to ans.

Implementation​

Live Editor
function Solution(arr) {
var clearStars = function(s) {
const mp = new Map();
    const n = s.length;
    const v = new Array(n).fill(0);

    for (let i = 0; i < n; i++) {
        if (s[i] !== '*') {
            if (!mp.has(s[i])) {
                mp.set(s[i], []);
            }
            mp.get(s[i]).push(i);
        } else {
            v[i] = 1;
            const sortedEntries = Array.from(mp.entries()).sort((a, b) => a[0].localeCompare(b[0]));
            for (let [key, indices] of sortedEntries) {
                const m = indices.length;
                v[indices[m - 1]] = 1;
                indices.pop();
                if (indices.length === 0) {
                    mp.delete(key);
                }
                break;
            }
        }
    }

    let ans = "";
    for (let i = 0; i < n; i++) {
        if (v[i] !== 1) {
            ans += s[i];
        }
    }
    return ans;
};
  const input = "aaba*"
  const output =clearStars(input)
  return (
    <div>
      <p>
        <b>Input: </b>
        {JSON.stringify(input)}
      </p>
      <p>
        <b>Output:</b> {output.toString()}
      </p>
    </div>
  );
}
Result
Loading...

Complexity Analysis​

  • Time Complexity: O(nlogn)O(nlogn) because of Nested Loops
  • Space Complexity: O(n)O(n) because of prefix array

Code in Different Languages​

Written by @hiteshgahanolia
var clearStars = function(s) {
const mp = new Map();
const n = s.length;
const v = new Array(n).fill(0);

for (let i = 0; i < n; i++) {
if (s[i] !== '*') {
if (!mp.has(s[i])) {
mp.set(s[i], []);
}
mp.get(s[i]).push(i);
} else {
v[i] = 1;
const sortedEntries = Array.from(mp.entries()).sort((a, b) => a[0].localeCompare(b[0]));
for (let [key, indices] of sortedEntries) {
const m = indices.length;
v[indices[m - 1]] = 1;
indices.pop();
if (indices.length === 0) {
mp.delete(key);
}
break;
}
}
}

let ans = "";
for (let i = 0; i < n; i++) {
if (v[i] !== 1) {
ans += s[i];
}
}
return ans;
};

References​