Lexicographically smallest subsequence possible by removing a character from given string | C ++ program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the string ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void firstSubsequence ( string s ) { int isMax = -1 ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s . erase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; } cout << s ; } int main ( ) { string S = " geeksforgeeks " ; firstSubsequence ( S ) ; return 0 ; }