-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_784_18.java
More file actions
60 lines (53 loc) · 1.53 KB
/
LeetCode_784_18.java
File metadata and controls
60 lines (53 loc) · 1.53 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
54
55
56
57
58
59
60
package Week_04.id_18;
import java.util.ArrayList;
import java.util.List;
/**
* @author LiveForExperience
* @date 2019/6/27 12:47
*/
public class LeetCode_784_18 {
List<String> list = new ArrayList<>();
public List<String> letterCasePermutation(String S) {
dfs(new StringBuilder(S), 0);
return list;
}
private void dfs(StringBuilder sb, int index) {
if (index == sb.length()) {
list.add(sb.toString());
return;
}
if (Character.isDigit(sb.charAt(index))) {
dfs(sb, index + 1);
} else {
char c = sb.charAt(index);
sb.setCharAt(index, Character.toLowerCase(c));
dfs(sb, index + 1);
sb.setCharAt(index, Character.toUpperCase(c));
dfs(sb, index + 1);
}
}
public List<String> letterCasePermutation1(String S) {
dfs(S.toCharArray(), 0);
return list;
}
private void dfs(char[] cs, int index) {
if (index == cs.length) {
list.add(new String(cs));
return;
}
if (cs[index] < 'A') {
dfs(cs, index + 1);
} else {
char c = cs[index];
if (c > 64 && c < 91) {
dfs(cs, index + 1);
cs[index] = (char) (cs[index] + 'a' - 'A');
dfs(cs, index + 1);
} else {
dfs(cs, index + 1);
cs[index] = (char) (cs[index] + 'A' - 'a');
dfs(cs, index + 1);
}
}
}
}