Lexicographically largest sub | C ++ implementation of the approach ; Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getSubSeq ( string s , int n ) { string res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s [ cr ] ; for ( int i = cr + 1 ; i < n ; i ++ ) mx = max ( mx , s [ i ] ) ; int lst = cr ; for ( int i = cr ; i < n ; i ++ ) if ( s [ i ] == mx ) { res += s [ i ] ; lst = i ; } cr = lst + 1 ; } return res ; } int main ( ) { string s = " geeksforgeeks " ; int n = s . length ( ) ; cout << getSubSeq ( s , n ) ; }