text: Form minimum number from given sequence | C # program to print minimum number that can be formed from a given sequence of Is and Ds ; min_avail represents the minimum number which is still available for inserting in the output vector . pos_of_I keeps track of the most recent index where ' I ' was encountered w . r . t the output vector ; vector to store the output ; cover the base cases ; Traverse rest of the input ; print the number ; Driver code
code: using System ; using System . Collections . Generic ; class GFG { static void printLeast ( String arr ) { int min_avail = 1 , pos_of_I = 0 ; List < int > al = new List < int > ( ) ; if ( arr [ 0 ] == ' I ' ) { al . Add ( 1 ) ; al . Add ( 2 ) ; min_avail = 3 ; pos_of_I = 1 ; } else { al . Add ( 2 ) ; al . Add ( 1 ) ; min_avail = 3 ; pos_of_I = 0 ; } for ( int i = 1 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == ' I ' ) { al . Add ( min_avail ) ; min_avail ++ ; pos_of_I = i + 1 ; } else { al . Add ( al [ i ] ) ; for ( int j = pos_of_I ; j <= i ; j ++ ) al [ j ] = al [ j ] + 1 ; min_avail ++ ; } } for ( int i = 0 ; i < al . Count ; i ++ ) Console . Write ( al [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { printLeast ( " IDID " ) ; printLeast ( " I " ) ; printLeast ( " DD " ) ; printLeast ( " II " ) ; printLeast ( " DIDI " ) ; printLeast ( " IIDDD " ) ; printLeast ( " DDIDDIID " ) ; } }