Lexicographically smallest string formed by removing duplicates

Given a string S consisting of lowercase alphabets, the task is to find the lexicographically smallest string that can be obtained by removing duplicates from the given string S.

Examples:

Input: S = “yzxyz”
Output: xyz
Explanation: Removing the duplicate characters at indices 0 and 1 in the given string, the remaining string “xyz” consists only of unique alphabets only and is the smallest possible string in lexicographical order.

Input: S = “acbc”
Output: “abc”
Explanation: Removing the duplicate characters at index 3 in the given string, the remaining string “abc” consists only of unique alphabets only and is the smallest possible string in lexicographical order.

Approach: Follow the steps below to solve the problem:



  • Initialize a string res to store the resultant string.
  • Store the frequency of each character present in the given string in an array freq[].
  • Maintain an array vis[] for marking the characters that are already present in the resultant string res.
  • Traverse the given string S and for each character S[i], perform the following:
    • Decrease the frequency of the current character by 1.
    • If the current character is not marked visited in the array vis[], then perform the following:
      • If the last letter of res is less than S[i], add S[i] to res.
      • If the last letter of res is greater than S[i] and the count of the last letter of res exceeds 0, then remove that character and mark visit[S[i]] as 0 and continue this step till the above condition is satisfied.
      • After breaking out from the above condition, add S[i] to res and mark visit[S[i]] as 1.
  • After completing the above steps, print the string res as the result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that finds lexicographically
// smallest string after removing the
// duplicates from the given string
string removeDuplicateLetters(string s)
{
    // Stores the frequency of characters
    int cnt[26] = { 0 };
  
    // Mark visited characters
    int vis[26] = { 0 };
  
    int n = s.size();
  
    // Stores count of each character
    for (int i = 0; i < n; i++)
        cnt[s[i] - 'a']++;
  
    // Stores the resultant string
    string res = "";
  
    for (int i = 0; i < n; i++) {
  
        // Decrease the count of
        // current character
        cnt[s[i] - 'a']--;
  
        // If character is not already
        // in answer
        if (!vis[s[i] - 'a']) {
  
            // Last character > S[i]
            // and its count > 0
            while (res.size() > 0
                   && res.back() > s[i]
                   && cnt[res.back() - 'a'] > 0) {
  
                // Mark letter unvisited
                vis[res.back() - 'a'] = 0;
                res.pop_back();
            }
  
            // Add s[i] in res and
            // mark it visited
            res += s[i];
            vis[s[i] - 'a'] = 1;
        }
    }
  
    // Return the resultant string
    return res;
}
  
// Driver Code
int main()
{
    // Given string S
    string S = "acbc";
  
    // Function Call
    cout << removeDuplicateLetters(S);
  
    return 0;
}

chevron_right


Output:

abc

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.