Lexicographically smallest subsequence possible by removing a character from given string | Java 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
import java . util . * ; class GFG { static void firstSubsequence ( String s ) { int isMax = - 1 ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) { isMax = i ; break ; } } if ( isMax >= 0 ) { s = s . substring ( 0 , isMax ) + s . substring ( isMax + 1 ) ; s . rerase ( isMax , 1 ) ; } else { s . erase ( s . length ( ) - 1 , 1 ) ; s = s . substring ( 0 , s . length ( ) - 1 ) ; } System . out . print ( s ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; firstSubsequence ( S ) ; } }