Repository navigation
Expand file tree
/
Copy pathwordallsort784.java
More file actions
39 lines (37 loc) · 1.44 KB
/
Copy pathwordallsort784.java
File metadata and controls
39 lines (37 loc) · 1.44 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
import java.util.ArrayList;
import java.util.List;
public class wordallsort784 {
public static void main(String[] args) {
String s = "C";
wordallsort784 w = new wordallsort784();
List<String> result = w.letterCasePermutation(s);
for(String st : result) System.out.println(st);
}
public List<String> letterCasePermutation(String S) {
List<String> result = new ArrayList<>();
result.add(S);
char[] chr = S.toCharArray();
allsort(result, 0, S, S.length(), chr);
return result;
}
public void allsort(List<String> result, int i, String s, int len, char[] chr) {
if(i == len) {
if(!result.contains(new String(chr))) result.add(new String(chr));
}
else {
if(chr[i] >= 'a' && chr[i] <= 'z'){
allsort(result, i + 1, s, len, chr);
chr[i] = Character.toUpperCase(chr[i]);
allsort(result, i + 1, s, len, chr);
chr[i] = Character.toLowerCase(chr[i]);
}
else if(chr[i] >= 'A' && chr[i] <= 'Z') {
allsort(result, i + 1, s, len, chr);
chr[i] = Character.toLowerCase(chr[i]);
allsort(result, i + 1, s, len, chr);
chr[i] = Character.toUpperCase(chr[i]);
}
else allsort(result, i + 1, s, len, chr);
}
}
}