Print all permutation of a string using ArrayList | Java implementation of the approach ; 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
import java . util . ArrayList ; public class GFG { static void printArrayList ( ArrayList < String > arrL ) { arrL . remove ( " " ) ; for ( int i = 0 ; i < arrL . size ( ) ; i ++ ) System . out . print ( arrL . get ( i ) + " ▁ " ) ; } public static ArrayList < String > getPermutation ( String str ) { if ( str . length ( ) == 0 ) { ArrayList < String > empty = new ArrayList < > ( ) ; empty . add ( " " ) ; return empty ; } char ch = str . charAt ( 0 ) ; String subStr = str . substring ( 1 ) ; ArrayList < String > prevResult = getPermutation ( subStr ) ; ArrayList < String > Res = new ArrayList < > ( ) ; for ( String val : prevResult ) { for ( int i = 0 ; i <= val . length ( ) ; i ++ ) { Res . add ( val . substring ( 0 , i ) + ch + val . substring ( i ) ) ; } } return Res ; } public static void main ( String [ ] args ) { String str = " abc " ; printArrayList ( getPermutation ( str ) ) ; } }