-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAllLettersAreArrangedInCase.java
More file actions
48 lines (44 loc) · 1.42 KB
/
AllLettersAreArrangedInCase.java
File metadata and controls
48 lines (44 loc) · 1.42 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
package other;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。 示例:
* 输入: S = "a1b2"
* 输出: ["a1b2", "a1B2", "A1b2", "A1B2"]
* <p>
* 输入: S = "3z4"
* 输出: ["3z4", "3Z4"]
* <p>
* 输入: S = "12345"
* 输出: ["12345"]
* 注意:
* <p>
* S 的长度不超过12。
* S 仅由数字和字母组成。
* @Date: Created in 23:24 8/4/2020
* @Modified by:
*/
public class AllLettersAreArrangedInCase {
public List<String> letterCasePermutation(String S) {
List<StringBuilder> ans = new ArrayList();
ans.add(new StringBuilder());
for (char c : S.toCharArray()) {
int n = ans.size();
if (Character.isLetter(c)) {
for (int i = 0; i < n; ++i) {
ans.add(new StringBuilder(ans.get(i)));
ans.get(i).append(Character.toLowerCase(c));
ans.get(n + i).append(Character.toUpperCase(c));
}
} else {
for (int i = 0; i < n; ++i)
ans.get(i).append(c);
}
}
List<String> finalans = new ArrayList();
for (StringBuilder sb : ans)
finalans.add(sb.toString());
return finalans;
}
}