Lexicographically smallest string formed by removing at most one character | Java program to find the lexicographically smallest String by removing at most one character ; Function to return the smallest String ; iterate the String ; first point where s [ i ] > s [ i + 1 ] ; append the String without i - th character in it ; leave the last character ; Driver Code
class GFG { static String smallest ( String s ) { int l = s . length ( ) ; String ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s . charAt ( j ) ; } } return ans ; } } ans = s . substring ( 0 , l - 1 ) ; return ans ; } public static void main ( String [ ] args ) { String s = " abcda " ; System . out . println ( smallest ( s ) ) ; } }