Print all permutation of a string using ArrayList | C # 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
using System . Collections . Generic ; using System ; class GFG { static void printArrayList ( List < String > arrL ) { arrL . Remove ( " " ) ; for ( int i = 0 ; i < arrL . Count ; i ++ ) Console . Write ( arrL [ i ] + " ▁ " ) ; } public static List < String > getPermutation ( String str ) { if ( str . Length == 0 ) { List < String > empty = new List < String > ( ) ; empty . Add ( " " ) ; return empty ; } char ch = str [ 0 ] ; String subStr = str . Substring ( 1 ) ; List < String > prevResult = getPermutation ( subStr ) ; List < String > Res = new List < String > ( ) ; foreach ( String val in 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 ) ) ; } }