Generate permutations with only adjacent swaps allowed | PHP program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code
< ? php function findPermutations ( $ str , $ index , $ n ) { if ( $ index >= $ n || ( $ index + 1 ) >= $ n ) { echo $ str , " STRNEWLINE " ; return ; } findPermutations ( $ str , $ index + 1 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; findPermutations ( $ str , $ index + 2 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; } $ str = "12345" ; $ n = strlen ( $ str ) ; findPermutations ( $ str , 0 , $ n ) ; ? >