Lexicographically smallest string possible by inserting given character | C ++ Program to implement the above approach ; Function to obtain lexicographically smallest string possible by inserting character c in the string s ; Traverse the string ; If the current character is greater than the given character ; Insert the character before the greater character ; Return the string ; Append the character at the end ; Return the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string SmallestString ( string s , char c ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] > c ) { s . insert ( i , 1 , c ) ; return s ; } } s += c ; return s ; } int main ( ) { string S = " acd " ; char C = ' b ' ; cout << SmallestString ( S , C ) << endl ; return 0 ; }