Lexicographically smallest string formed repeatedly deleting character from substring 10 | Java program for the above approach ; Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the string S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the string str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Driver code ; Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static String lexicographicallySmallestString ( String S , int N ) { int LastZe = - 1 ; String ans = " " ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '0' ) { LastZe = i ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S . charAt ( i ) == '0' ) ans += S . charAt ( i ) ; else if ( i > LastZe ) ans += S . charAt ( i ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "11001101" ; int N = S . length ( ) ; System . out . println ( lexicographicallySmallestString ( S , N ) ) ; } }