Lexicographically largest subsequence containing all distinct characters only once
Last Updated :
30 May, 2022
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 lexicographically 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++
#include <bits/stdc++.h>
using namespace std;
string lexicoMaxSubsequence(string s, int n)
{
stack< char > st;
vector< int > visited(26, 0), cnt(26, 0);
for ( int i = 0; i < n; i++) {
cnt[s[i] - 'a' ]++;
}
for ( int i = 0; i < n; i++) {
cnt[s[i] - 'a' ]--;
if (visited[s[i] - 'a' ]) {
continue ;
}
while (!st.empty() && st.top() < s[i]
&& cnt[st.top() - 'a' ] != 0) {
visited[st.top() - 'a' ] = 0;
st.pop();
}
st.push(s[i]);
visited[s[i] - 'a' ] = 1;
}
string s1;
while (!st.empty()) {
s1 = st.top() + s1;
st.pop();
}
return s1;
}
int main()
{
string S = "ababc" ;
int N = S.length();
cout << lexicoMaxSubsequence(S, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static String lexicoMaxSubsequence(String s, int n)
{
Stack<Character> st = new Stack<Character>();
int [] visited = new int [ 26 ];
int [] cnt = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++) {
visited[i] = 0 ;
cnt[i] = 0 ;
}
for ( int i = 0 ; i < n; i++) {
cnt[s.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < n; i++) {
cnt[s.charAt(i) - 'a' ]--;
if (visited[s.charAt(i) - 'a' ] > 0 ) {
continue ;
}
while (!st.empty() && st.peek() < s.charAt(i)
&& cnt[st.peek() - 'a' ] != 0 ) {
visited[st.peek() - 'a' ] = 0 ;
st.pop();
}
st.push(s.charAt(i));
visited[s.charAt(i) - 'a' ] = 1 ;
}
String s1 = "" ;
while (!st.empty()) {
s1 = st.peek() + s1;
st.pop();
}
return s1;
}
public static void main(String[] args)
{
String S = "ababc" ;
int N = S.length();
System.out.println(lexicoMaxSubsequence(S, N));
}
}
|
Python3
def lexicoMaxSubsequence(s, n):
st = []
visited = [ 0 ] * ( 26 )
cnt = [ 0 ] * ( 26 )
for i in range ( 26 ):
visited[i] = 0
cnt[i] = 0
for i in range (n):
cnt[ ord (s[i]) - ord ( 'a' )] + = 1
for i in range (n):
cnt[ ord (s[i]) - ord ( 'a' )] - = 1
if (visited[ ord (s[i]) - ord ( 'a' )] > 0 ):
continue
while ( len (st) > 0 and ord (st[ - 1 ]) < ord (s[i]) and cnt[ ord (st[ - 1 ]) - ord ( 'a' )] ! = 0 ):
visited[ ord (st[ - 1 ]) - ord ( 'a' )] = 0
st.pop()
st.append(s[i])
visited[ ord (s[i]) - ord ( 'a' )] = 1
s1 = ""
while ( len (st) > 0 ):
s1 = st[ - 1 ] + s1
st.pop()
return s1
S = "ababc"
N = len (S)
print (lexicoMaxSubsequence(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string lexicoMaxSubsequence( string s, int n)
{
Stack< char > st = new Stack< char >();
int [] visited = new int [26];
int [] cnt = new int [26];
for ( int i = 0; i < 26; i++) {
visited[i] = 0;
cnt[i] = 0;
}
for ( int i = 0; i < n; i++) {
cnt[s[i] - 'a' ]++;
}
for ( int i = 0; i < n; i++) {
cnt[s[i] - 'a' ]--;
if (visited[s[i] - 'a' ] > 0) {
continue ;
}
while (st.Count > 0 && st.Peek() < s[i]
&& cnt[st.Peek() - 'a' ] != 0) {
visited[st.Peek() - 'a' ] = 0;
st.Pop();
}
st.Push(s[i]);
visited[s[i] - 'a' ] = 1;
}
string s1 = "" ;
while (st.Count > 0) {
s1 = st.Peek() + s1;
st.Pop();
}
return s1;
}
static void Main() {
string S = "ababc" ;
int N = S.Length;
Console.Write(lexicoMaxSubsequence(S, N));
}
}
|
Javascript
<script>
function lexicoMaxSubsequence(s, n)
{
let st = [];
let visited = new Array(26);
let cnt = new Array(26);
for (let i = 0; i < 26; i++) {
visited[i] = 0;
cnt[i] = 0;
}
for (let i = 0; i < n; i++) {
cnt[s[i].charCodeAt() - 'a' .charCodeAt()]++;
}
for (let i = 0; i < n; i++) {
cnt[s[i].charCodeAt() - 'a' .charCodeAt()]--;
if (visited[s[i].charCodeAt() - 'a' .charCodeAt()] > 0) {
continue ;
}
while (st.length > 0 && st[st.length - 1].charCodeAt() < s[i].charCodeAt()
&& cnt[st[st.length - 1].charCodeAt() - 'a' .charCodeAt()] != 0) {
visited[st[st.length - 1].charCodeAt() - 'a' .charCodeAt()] = 0;
st.pop();
}
st.push(s[i]);
visited[s[i].charCodeAt() - 'a' .charCodeAt()] = 1;
}
let s1 = "" ;
while (st.length > 0) {
s1 = st[st.length - 1] + s1;
st.pop();
}
return s1;
}
let S = "ababc" ;
let N = S.length;
document.write(lexicoMaxSubsequence(S, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...