Skip to content

Commit b98642e

Browse files
Merge pull request #3 from souravpalitrana/feature/permutation_alternate
Add the solution of permutation duplicate and also alternate solution
2 parents 13ee2b7 + 475b44d commit b98642e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Recursion/Permutation/src/permutation/PermutationAlternate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public class PermutationAlternate {
1515
* @param args the command line arguments
1616
*/
1717
public static void main(String[] args) {
18-
String input = "ABCD";
18+
String input = "ABC";
1919

2020
calculate(input, 0, input.length());
2121
}
2222

2323
public static void calculate(String input, int left, int right) {
24-
if (left == right) {
24+
if (left == right - 1) {
2525
System.out.println(input);
2626
} else {
2727
for (int i = left; i < right; i++) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package permutation;
7+
8+
/**
9+
*
10+
* @author souravpalit
11+
*/
12+
public class PermutationWithDuplicateAlternate {
13+
14+
/**
15+
* @param args the command line arguments
16+
*/
17+
public static void main(String[] args) {
18+
String input = "AAB";
19+
20+
calculate(input, 0, input.length());
21+
}
22+
23+
public static void calculate(String input, int left, int right) {
24+
if (left == right) {
25+
System.out.println(input);
26+
} else {
27+
for (int i = left; i < right; i++) {
28+
if (i != 0 && input.charAt(i) == input.charAt(i -1)) {
29+
continue;
30+
} else {
31+
String swapped = swap(input, left, i);
32+
calculate(swapped, left + 1, right);
33+
}
34+
}
35+
}
36+
}
37+
38+
public static String swap(String input, int first, int second) {
39+
char [] inputChars = input.toCharArray();
40+
char temp = inputChars[first];
41+
inputChars[first] = inputChars[second];
42+
inputChars[second] = temp;
43+
return String.valueOf(inputChars);
44+
}
45+
}

0 commit comments

Comments
 (0)