-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPermutationOfString.java
39 lines (32 loc) · 1.14 KB
/
PermutationOfString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.leetcode.year_2020.backtracking;
import com.util.LogUtil;
import java.util.HashSet;
import java.util.Set;
/**
* Created by jaine03 on 23/07/17.
*/
public class PermutationOfString {
public static void main(String[] args) {
printAllPermutations("ABC", "");
LogUtil.newLine();
printAllPermutations("AABC", "");
}
private static void printAllPermutations(String input, String output) {
// Base condition
if (input.length() == 0) {
System.out.println(output);
return;
}
// To avoid duplicates, we should have a Map which will hold what all we explored
final Set<Character> explored = new HashSet<>();
// Choices and Decisions
for (int i = 0; i < input.length(); i++) {
if (!explored.contains(input.charAt(i))) {
explored.add(input.charAt(i));
String newInput = input.substring(0, i) + input.substring(i + 1); // Skipping the iTh character
String newOutput = output + input.charAt(i);
printAllPermutations(newInput, newOutput);
}
}
}
}