Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c121444

Browse files
Added PermutationOfString code file
1 parent ce8de8c commit c121444

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)

AltStyle によって変換されたページ (->オリジナル) /