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++
// 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; } |
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.
Recommended Posts:
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest subsequence possible by removing a character from given string
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Character replacement after removing duplicates from a string
- Check if string is palindrome after removing all consecutive duplicates
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically largest string formed from the characters in range L and R
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Find longest palindrome formed by removing or shuffling chars from string
- Removing row or column wise duplicates from matrix of characters
- K-th lexicographically smallest unique substring of a given string
- Lexicographically smallest string obtained after concatenating array
- Lexicographically smallest permutation of a string with given subsequences
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
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.

