Examples:
Input: S = “a1b2”
Output: [“a1b2”, “a1B2”, “A1b2”, “A1B2”]
Input: S = “3z4”
Output: [“3z4”, “3Z4”]
Input: S = “12345”
Output: [“12345”]
class Solution {
public List<String> letterCasePermutation(String S) {
List<String> results = new ArrayList<String>();
char[] list = S.toCharArray();
helper(results, list, 0);
return results;
}
public void helper(List<String> results, char[] list, int n){
if(n == list.length){
results.add(new String(list));
return;
}
if(list[n] >= '0' && list[n] <= '9'){
helper(results, list, n + 1);
return;
}
list[n] = Character.toLowerCase(list[n]);
helper(results, list, n + 1);
list[n] = Character.toUpperCase(list[n]);
helper(results, list, n + 1);
}
}
总结
哎这道题也不是一下子做出来的。忘了还有这么个方法Character.toLowerCase()。
而且1-9的时候就直接深搜就可以了。