-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path784.java
More file actions
57 lines (48 loc) · 1.88 KB
/
784.java
File metadata and controls
57 lines (48 loc) · 1.88 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
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public List<String> letterCasePermutation(String S) {
List<String> list = new ArrayList<>();
helper(S.toCharArray(),0,list);
return list;
}
private void helper(char[] a, int pos, List<String> res){
if(pos==a.length){
res.add(new String(a));
return;
}
if(Character.isLetter(a[pos])) {
a[pos] = Character.toLowerCase(a[pos]);
helper(a, pos+1, res);
a[pos] = Character.toUpperCase(a[pos]);
}
helper(a, pos+1, res);
}
}
__________________________________________________________________________________________________
sample 35924 kb submission
import static java.util.stream.Collectors.toList;
class Solution {
public List<String> letterCasePermutation(String s) {
List<StringBuilder> result = new ArrayList<>();
result.add(new StringBuilder());
for (char c: s.toCharArray()){
char lower = Character.toLowerCase(c);
char upper = Character.toUpperCase(c);
if (lower == upper){
for (StringBuilder sb: result){
sb.append(lower);
}
} else {
List<StringBuilder> withUpper = new ArrayList<>();
for (StringBuilder sb: result){
withUpper.add(new StringBuilder(sb).append(upper));
sb.append(lower);
}
result.addAll(withUpper);
}
}
return result.stream().map(sb -> sb.toString()).collect(toList());
}
}
__________________________________________________________________________________________________