Print all possible strings that can be made by placing spaces | Function recursively prints the strings having space pattern . i and j are indices in ' str [ ] ' and ' buff [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; This function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations . ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver code
< ? php function printPatternUtil ( $ str , $ buff , $ i , $ j , $ n ) { if ( $ i == $ n ) { $ buff [ $ j ] = ' ' ; echo str_replace ( ' , ▁ ' , ' ' , implode ( ' , ▁ ' , $ buff ) ) . " STRNEWLINE " ; return ; } $ buff [ $ j ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i + 1 , $ j + 1 , $ n ) ; $ buff [ $ j ] = ' ▁ ' ; $ buff [ $ j + 1 ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i +1 , $ j + 2 , $ n ) ; } function printPattern ( $ str ) { $ n = strlen ( $ str ) ; $ buf = array_fill ( 0 , 2 * $ n , null ) ; $ buf [ 0 ] = $ str [ 0 ] ; printPatternUtil ( $ str , $ buf , 1 , 1 , $ n ) ; } $ str = " ABCD " ; printPattern ( $ str ) ; ? >