File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Practice Problems/Backtracking/PermutationsOfString Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+
3
+ public class PermutationsOfString {
4
+
5
+ public static void main (String args []) {
6
+
7
+ String input = "ABC" ;
8
+ HashSet <String > resultSet = new HashSet <String >();
9
+
10
+ permutation (input , 0 , resultSet );
11
+
12
+ for (String str : resultSet ) {
13
+ System .out .println (str );
14
+ }
15
+
16
+ }
17
+
18
+ public static void permutation (String input , int fixedIndex , HashSet <String > resultSet ) {
19
+
20
+
21
+ if (fixedIndex == input .length ()-1 ) {
22
+ resultSet .add (input );
23
+ return ;
24
+ }
25
+
26
+ for (int i = fixedIndex ; i < input .length (); i ++) {
27
+
28
+ input = swap (input , fixedIndex , i );
29
+ resultSet .add (input );
30
+ permutation (input , fixedIndex +1 , resultSet );
31
+ input = swap (input , fixedIndex , i );
32
+
33
+ }
34
+
35
+ }
36
+
37
+ public static String swap (String s , int i , int j ) {
38
+
39
+ char [] input = s .toCharArray ();
40
+
41
+ char temp = input [i ];
42
+ input [i ] = input [j ];
43
+ input [j ] = temp ;
44
+
45
+ return String .valueOf (input );
46
+ }
47
+
48
+ }
You can’t perform that action at this time.
0 commit comments