File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * A string of length n has n! permutation.
3
+ */
4
+
5
+ import java .util .*;
6
+ import java .io .*;
7
+
8
+ class AllPermutationsOfAString {
9
+
10
+ public static void main (String [] args ){
11
+ Scanner sc = new Scanner (System .in );
12
+ String S = sc .nextLine ();
13
+ int len = S .length ();
14
+ AllPermutationsOfAString per = new AllPermutationsOfAString ();
15
+ per .permute (S , 0 , len -1 );
16
+
17
+ }
18
+
19
+ public void permute (String S , int l , int r ){
20
+ if (l == r )
21
+ System .out .println (S );
22
+ else {
23
+ for (int i =l ; i <=r ; i ++){
24
+ S = swap (S , l , i );
25
+ permute (S , l +1 , r );
26
+ S = swap (S , l , i );
27
+ }
28
+ }
29
+ }
30
+
31
+ //Swapping characters of a string.
32
+ public String swap (String a , int i , int j )
33
+ {
34
+ char temp ;
35
+ char [] charArray = a .toCharArray ();
36
+ temp = charArray [i ] ;
37
+ charArray [i ] = charArray [j ];
38
+ charArray [j ] = temp ;
39
+ return String .valueOf (charArray );
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments