Lexicographically smallest string formed by removing at most one character | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string smallest ( string s ) { int l = s . length ( ) ; string ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) ans += s [ j ] ; } return ans ; } } ans = s . substr ( 0. , l - 1 ) ; return ans ; } int main ( ) { string s = " abcda " ; cout << smallest ( s ) ; return 0 ; }