Skip to content

Commit f9d80e6

Browse files
authored
Merge pull request #2 from prashant3360/prashant3360-patch-2
Added PermutationOfString code file
2 parents ce8de8c + c121444 commit f9d80e6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)