1+ /*Implementation of java program to print all the permutations of a string
2+ * With duplicates allowed
3+
4+ This program finds all the possible permutation of the string using backtracking.
5+
6+ A permutation, also called an “arrangement number” or “order,” is a rearrangement
7+ of the elements of an ordered list S into a one-to-one correspondence with S itself.
8+ A string of length n has n! permutation.*/
9+
10+ import java .util .*;
11+ import java .lang .*;
12+ import java .io .*;
13+
14+ public class Print_all_permutations_of_string
15+ {
16+
17+ //This function prints all the permutations of the string using Backtracking
18+ //It contains 3 parameters -
19+ // 1. Character array of given string
20+ // 2. Starting index of string
21+ // 3. Ending index of string
22+ public void permutation (char [] charArr , int start , int size )
23+ {
24+ int x ;
25+ if (start == size ) //condition to check whether the char array is completely traversed or not
26+ {
27+ for (int i = 0 ; i < charArr .length ; i ++)
28+ {
29+ System .out .print (charArr [i ]); //printing of permutations
30+ }
31+ System .out .println ();
32+ }
33+ else
34+ {
35+ for (x = start ; x < size ; x ++)
36+ {
37+ swap (charArr , start , x ); //function call to swap
38+ permutation (charArr , start + 1 , size ); //recursion call
39+ swap (charArr , start , x );
40+ }
41+ }
42+ }
43+
44+
45+ //This parameterized function swaps the ith and jth position from the character array
46+ //It contains 3 parameters - Character array and ith and jth postion in it
47+ public void swap (char [] charArr , int i , int j )
48+ {
49+ char temp = charArr [i ];
50+ charArr [i ] = charArr [j ];
51+ charArr [j ] = temp ;
52+ }
53+
54+ public static void main (String [] args ) throws java .lang .Exception
55+ {
56+ Scanner sc =new Scanner (System .in );
57+ String s = sc .nextLine (); //taking a string as an input
58+ char [] arr = s .toCharArray (); //storing all the characters in the string in a character array
59+ Print_all_permutations_of_String obj = new Print_all_permutations_of_String ();
60+ obj .permutation (arr , 0 , arr .length ); //function call to print the permutations of string
61+ }
62+ }
63+
64+ /*
65+ *
66+ * Sample Input 1: abc
67+ *
68+ * Sample Output 1:
69+ *
70+ * abc
71+ * acb
72+ * bac
73+ * bca
74+ * cba
75+ * cab
76+ *
77+ * Sample Input 2: Word
78+ *
79+ * Sample Output 2:
80+ *
81+ * Word
82+ * Wodr
83+ * Wrod
84+ * Wrdo
85+ * Wdro
86+ * Wdor
87+ * oWrd
88+ * oWdr
89+ * orWd
90+ * ordW
91+ * odrW
92+ * odWr
93+ * roWd
94+ * rodW
95+ * rWod
96+ * rWdo
97+ * rdWo
98+ * rdoW
99+ * dorW
100+ * doWr
101+ * droW
102+ * drWo
103+ * dWro
104+ * dWor
105+ *
106+ *
107+ * Time and Space complexity -
108+ *
109+ * For any given string of length n there are n! possible permutations,
110+ and we need to print all of them so Time complexity is O(n * n!).
111+
112+ * The function will be called recursively and will be stored in call
113+ stack for all n! permutations, so Space complexity is O(n!).
114+
115+ */
0 commit comments