The Wayback Machine - https://web.archive.org/web/20210819001928/https://www.geeksforgeeks.org/lexicographically-largest-subsequence-containing-all-distinct-characters-only-once/
Skip to content
Related Articles

Related Articles

Improve Article

Lexicographically largest subsequence containing all distinct characters only once

  • Last Updated : 18 Aug, 2021

Given a string S, the task is to find the lexicographically largest subsequence that can be formed using all distinct characters only once from the given string.

Examples:

Input: S = ababc
Output: bac
Explanation:
All possible subsequences containing all the characters in S exactly once are {“abc”, “bac”}. The lexicograohically maximum among all the subsequences  is “bac”.

Input: S = “zydsbacab”
Output: zydscab

Approach: The given problem can be solved using Stack. The idea is to traverse the string and store those characters in the stack in lexicographically largest order so as to generate the resultant string. Follow the steps below to solve the given problem:



Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the lexicographically
// largest subsequence consisting of all
// distinct characters of S only once
string lexicoMaxSubsequence(string s, int n)
{
    stack<char> st;
  
    // Stores if any alphabet is present
    // in the current stack
    vector<int> visited(26, 0), cnt(26, 0);
  
    // Findthe number of occurences of
    // the character s[i]
    for (int i = 0; i < n; i++) {
        cnt[s[i] - 'a']++;
    }
    for (int i = 0; i < n; i++) {
  
        // Decrease the character count
        // in remaining string
        cnt[s[i] - 'a']--;
  
        // If character is already present
        // in the stack
        if (visited[s[i] - 'a']) {
            continue;
        }
  
        // if current character is greater
        // than last character in stack
        // then pop the top character
        while (!st.empty() && st.top() < s[i]
               && cnt[st.top() - 'a'] != 0) {
            visited[st.top() - 'a'] = 0;
            st.pop();
        }
  
        // Push the current character
        st.push(s[i]);
        visited[s[i] - 'a'] = 1;
    }
  
    // Stores the resultant string
    string s1;
  
    // Generate the string
    while (!st.empty()) {
        s1 = st.top() + s1;
        st.pop();
    }
  
    // Return the resultant string
    return s1;
}
  
// Driver Code
int main()
{
    string S = "ababc";
    int N = S.length();
    cout << lexicoMaxSubsequence(S, N);
  
    return 0;
}
Output:
bac

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :