
正文
[LeetCode] 784. 字母大小写全排列 ☆☆☆(回溯、深度优先遍历)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
https://leetcode-cn.com/problems/letter-case-permutation/solution/shen-du-you-xian-bian-li-hui-su-suan-fa-python-dai/
描述
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
示例:
输入: S = "a1b2"
输出: ["a1b2", "a1B2", "A1b2", "A1B2"]
输入: S = "3z4"
输出: ["3z4", "3Z4"]
输入: S = "12345"
输出: ["12345"]
注意:
S 的长度不超过12。
S 仅由数字和字母组成。
解析
明显的回溯。有字母的char,有2个分支。
可以想象为二叉树,深度优先遍历每一颗子树。
技巧:大小写字母转换
使用异或运算转换字母大小写。
我们发现大写字符与其对应的小写字符的 ASCII 的差为 32,32 这个值如果敏感的话,它是 2^5
,在编程语言中,可以表示为 1 << 5。而
变换大小写这件事等价于:
1、如果字符是小写字符,减去 32 得到大写字符;
2、如果字符是大写字符,加上 32 得到小写字符。
而这两者合并起来,就是给这个字符做一次不进位的加法,即异或上 1 << 5。
代码class Solution {
public List<String> letterCasePermutation(String S) {
List<String> list = new ArrayList<>();
if (S == null || S.length() <= 0) {
return list;
}
// letterCasePermutation(S, "", list, 0);
letterCasePermutation1(S.toCharArray(), list, 0);
return list;
}
//只有2个分支,判断一下
public static void letterCasePermutation1(char[] chars, List<String> list, int start) {
if (start == chars.length) {
list.add(new String(chars));
return;
}
letterCasePermutation1(chars, list, start + 1);
if (Character.isLetter(chars[start])) {
chars[start] ^= (1 << 5);
letterCasePermutation1(chars, list, start + 1);
}
}
//me
public void letterCasePermutation(String S, String tempStr, List<String> list, int start) {
if (start == S.length() && tempStr.length() == S.length()) {
list.add(tempStr);
}
for (int i = start; i < S.length(); i++) {
char curChar = S.charAt(i);
if (curChar >= 48 && curChar <= 57) {
tempStr += curChar;
start++;
} else {
for (int k = 0; k < 2; k++) {
if (k == 0) {
tempStr += String.valueOf(curChar).toLowerCase();
} else {
tempStr += String.valueOf(curChar).toUpperCase();
}
letterCasePermutation(S, tempStr, list, i + 1);
tempStr = tempStr.substring(0, tempStr.length() - 1);
}
} if (start == S.length() && tempStr.length() == S.length()) {
list.add(tempStr);
}
}
}
}
class Solution {
public List<String> letterCasePermutation(String S) {
List<String> list = new ArrayList<>();
if (S == null || S.length() <= 0) {
return list;
}
// letterCasePermutation(S, "", list, 0);
letterCasePermutation1(S.toCharArray(), list, 0);
return list;
}
//只有2个分支,判断一下
public static void letterCasePermutation1(char[] chars, List<String> list, int start) {
if (start == chars.length) {
list.add(new String(chars));
return;
}
letterCasePermutation1(chars, list, start + 1);
if (Character.isLetter(chars[start])) {
chars[start] ^= (1 << 5);
letterCasePermutation1(chars, list, start + 1);
}
}
//me
public void letterCasePermutation(String S, String tempStr, List<String> list, int start) {
if (start == S.length() && tempStr.length() == S.length()) {
list.add(tempStr);
}
for (int i = start; i < S.length(); i++) {
char curChar = S.charAt(i);
if (curChar >= 48 && curChar <= 57) {
tempStr += curChar;
start++;
} else {
for (int k = 0; k < 2; k++) {
if (k == 0) {
tempStr += String.valueOf(curChar).toLowerCase();
} else {
tempStr += String.valueOf(curChar).toUpperCase();
}
letterCasePermutation(S, tempStr, list, i + 1);
tempStr = tempStr.substring(0, tempStr.length() - 1);
}
} if (start == S.length() && tempStr.length() == S.length()) {
list.add(tempStr);
}
}
}
}






