784. Letter Case Permutation
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> list = new ArrayList<>();
dfs(S, new StringBuilder(), list, 0);
return list;
}
public void dfs(String s, StringBuilder sb, List<String> list, int pos) {
if (pos == s.length()) {
list.add(sb.toString());
return;
}
char c = s.charAt(pos);
if (Character.isDigit(c)) {
sb.append(c);
dfs(s, sb, list, pos + 1);
} else {
sb.append(Character.toLowerCase(c));
dfs(s, sb, list, pos + 1);
sb.deleteCharAt(sb.length() - 1);
sb.append(Character.toUpperCase(c));
dfs(s, sb, list, pos + 1);
}
sb.deleteCharAt(sb.length() - 1);
}
}Last updated