Skip to content

Commit dfc0eb6

Browse files
authored
Merge pull request #83 from prashant3360/master
Submission for the Problem " All Permutation of a given String "
2 parents e277837 + f9d80e6 commit dfc0eb6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Permutations of a given string
2+
3+
**Problem from GeeksForGeeks**
4+
5+
A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
6+
Your task is to print all permutations of a given `input` string in new line.
7+
8+
### Examples:
9+
10+
#### 1
11+
12+
**Input** :
13+
```java
14+
input = "ABC"
15+
```
16+
17+
**Output:** :
18+
```java
19+
ACB
20+
BCA
21+
ABC
22+
CBA
23+
BAC
24+
CAB
25+
```
26+

0 commit comments

Comments
 (0)