-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (37 loc) · 1.06 KB
/
Main.java
File metadata and controls
53 lines (37 loc) · 1.06 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
class Main {
static int combinationsInt = 0;
static ArrayList<String> combinationsString = new ArrayList<String>();
public static void main(String[] args) {
String word = "Max";
int n = word.length();
Main main = new Main();
main.scrambleWord(word, 0, n-1);
System.out.println(combinationsInt);
}
public Main(){
}
private void scrambleWord(String str, int l, int r){
if (l == r){
if(!combinationsString.contains(str)){
System.out.println(str);
combinationsInt += 1;
combinationsString.add(str);
}
}else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
scrambleWord(str, l + 1, r);
str = swap(str, l, i);
}
}
}
private String swap(String word, int i, int j){
char temp;
char[] charArray = word.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
}