Description
Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Examples
Example 1:
Input: s = “a1b2”
Output: [“a1b2”,“a1B2”,“A1b2”,“A1B2”]
Example 2:
Input: s = “3z4”
Output: [“3z4”,“3Z4”]
Constraints:
1 <= s.length <= 12
s consists of lowercase English letters, uppercase English letters, and digits.
思路
就是一个DFS的思路,判断当前字符是否为数字,如果不是数字,就有2种可能性,然后往后继续
代码
class Solution {
public List<String> helper(String s, int start) {
if (start == s.length() - 1) {
List<String> tmp = new ArrayList<>();
tmp.add(s.substring(start));
if (s.charAt(start) >= '0' && s.charAt(start) <='9')
return tmp;
tmp.add(String.valueOf((char)(s.charAt(start) - 32)));
return tmp;
}
List<String> append = helper(s, start + 1);
List<String> answer = new ArrayList<>();
if (s.charAt(start) >= '0' && s.charAt(start) <= '9'){
for (String app: append) {
answer.add(s.charAt(start) + app);
}
return answer;
}
for (String app: append) {
answer.add(String.valueOf(s.charAt(start)) + app);
answer.add(String.valueOf((char)(s.charAt(start) - 32)) + app);
}
return answer;
}
public List<String> letterCasePermutation(String s) {
s = s.toLowerCase();
return helper(s, 0);
}
}