Lexicographically smallest string formed repeatedly deleting character from substring 10 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexicographicallySmallestString ( string S , int N ) { int LastZe = -1 ; string ans ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '0' ) { LastZe = i ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S [ i ] == '0' ) ans += S [ i ] ; else if ( i > LastZe ) ans += S [ i ] ; } return ans ; } int main ( ) { string S = "11001101" ; int N = S . size ( ) ; cout << lexicographicallySmallestString ( S , N ) ; return 0 ; }