text: Form minimum number from given sequence | Prints the minimum number that can be formed from input sequence of I ' s ▁ and ▁ D ' s ; Initialize current_max ( to make sure that we don 't use repeated  character ; Initialize last_entry ( Keeps track for last printed digit ) ; Iterate over input array ; Initialize ' noOfNextD ' to get count of next D 's available ; If letter is ' I ' Calculate number of next consecutive D 's  available ; If ' I ' is first letter , print incremented sequence from 1 ; Set max digit reached ; If not first letter Get next digit to print ; Print digit for I ; For all next consecutive ' D ' print decremented sequence ; If letter is ' D ' ; If ' D ' is first letter in sequence . Find number of Next D 's available ; Calculate first digit to print based on number of consecutive D 's ; Print twice for the first time ; Store last entry ; If current ' D ' is not first letter Decrement last_entry ; Driver Code
code: < ? php function PrintMinNumberForPattern ( $ arr ) { $ curr_max = 0 ; $ last_entry = 0 ; $ j ; for ( $ i = 0 ; $ i < strlen ( $ arr ) ; $ i ++ ) { $ noOfNextD = 0 ; switch ( $ arr [ $ i ] ) { case ' I ' : $ j = $ i + 1 ; while ( $ arr [ $ j ] == ' D ' && $ j < strlen ( $ arr ) ) { $ noOfNextD ++ ; $ j ++ ; } if ( $ i == 0 ) { $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , ++ $ last_entry ; echo " ▁ " , $ curr_max ; $ last_entry = $ curr_max ; } else { $ curr_max = $ curr_max + $ noOfNextD + 1 ; $ last_entry = $ curr_max ; echo " ▁ " , $ last_entry ; } for ( $ k = 0 ; $ k < $ noOfNextD ; $ k ++ ) { echo " ▁ " , -- $ last_entry ; $ i ++ ; } break ; case ' D ' : if ( $ i == 0 ) { $ j = $ i + 1 ; while ( ( $ arr [ $ j ] == ' D ' ) && ( $ j < strlen ( $ arr ) ) ) { $ noOfNextD ++ ; $ j ++ ; } $ curr_max = $ noOfNextD + 2 ; echo " ▁ " , $ curr_max , " ▁ " , $ curr_max - 1 ; $ last_entry = $ curr_max - 1 ; } else { echo " ▁ " , $ last_entry - 1 ; $ last_entry -- ; } break ; } } echo " STRNEWLINE " ; } PrintMinNumberForPattern ( " IDID " ) ; PrintMinNumberForPattern ( " I " ) ; PrintMinNumberForPattern ( " DD " ) ; PrintMinNumberForPattern ( " II " ) ; PrintMinNumberForPattern ( " DIDI " ) ; PrintMinNumberForPattern ( " IIDDD " ) ; PrintMinNumberForPattern ( " DDIDDIID " ) ; ? >