-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0784LetterCasePermutation.java
More file actions
78 lines (66 loc) · 2.46 KB
/
_0784LetterCasePermutation.java
File metadata and controls
78 lines (66 loc) · 2.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.heatwave.leetcode.problems;
import java.util.ArrayList;
import java.util.List;
public class _0784LetterCasePermutation {
static class Solution {
List<String> ans = new ArrayList<>();
public List<String> letterCasePermutation(String s) {
List<Character> temp = new ArrayList<>();
backtracking(s.toCharArray(), temp, 0);
return ans;
}
private void backtracking(char[] chars, List<Character> temp, int start) {
if (temp.size() == chars.length) {
StringBuilder sb = new StringBuilder();
for (Character character : temp) {
sb.append(character);
}
ans.add(sb.toString());
return;
}
for (int i = start; i < chars.length; i++) {
if (Character.isDigit(chars[i])) {
temp.add(chars[i]);
backtracking(chars, temp, i + 1);
temp.remove(temp.size() - 1);
continue;
}
temp.add(Character.toLowerCase(chars[i]));
backtracking(chars, temp, i + 1);
temp.remove(temp.size() - 1);
temp.add(Character.toUpperCase(chars[i]));
backtracking(chars, temp, i + 1);
temp.remove(temp.size() - 1);
}
}
}
static class SolutionBFS {
public List<String> letterCasePermutation(String s) {
List<String> list = new ArrayList<>();
list.add("");
char[] chars = s.toCharArray();
for (char c : chars) {
List<String> another = new ArrayList<>();
if (Character.isDigit(c)) {
for (String str : list) {
another.add(str + c);
}
list = another;
continue;
}
for (String str : list) {
another.add(str + Character.toLowerCase(c));
another.add(str + Character.toUpperCase(c));
}
list = another;
}
return list;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
SolutionBFS solutionDFS = new SolutionBFS();
List<String> list = solutionDFS.letterCasePermutation("a1b2c");
System.out.println(list);
}
}