Print all permutation of a string using ArrayList | Utility function to print the contents of the ArrayList ; Function to returns the arraylist which contains all the permutation of str ; If string is empty ; Return an empty arraylist ; Take first character of str ; Take sub - string starting from the second character ; Recurvise call ; Store the generated permutations into the resultant arraylist ; Return the resultant arraylist ; Driver code
function printArrayList ( arrL ) { for ( let i = 0 ; i < arrL . length ; i ++ ) document . write ( arrL [ i ] + " " ) ; } function getPermutation ( str ) { if ( str . length == 0 ) { let empty = [ ] ; empty . push ( " " ) ; return empty ; } let ch = str [ 0 ] ; let subStr = str . substring ( 1 ) ; let prevResult = getPermutation ( subStr ) ; let Res = [ ] ; for ( let val of prevResult ) { for ( let i = 0 ; i <= val . length ; i ++ ) { Res . push ( val . substring ( 0 , i ) + ch + val . substring ( i ) ) ; } } return Res ; } let str = " " ; printArrayList ( getPermutation ( str ) ) ;