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
using System ; class GFG { static String SmallestString ( String s , char c ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] > c ) { String temp = s ; s = s . Substring ( 0 , i ) ; s += c ; s += temp . Substring ( i , temp . Length - 1 ) ; return s ; } } s += c ; return s ; } public static void Main ( String [ ] args ) { String S = " acd " ; char C = ' b ' ; Console . WriteLine ( SmallestString ( S , C ) ) ; } }