一、题目
二、题解
枚举每一个位置,如果当前位置是字母就大小写转换后枚举两次
class Solution {
public:
bool isdigit(char x) {
return x <= '9' && x >= '0';
}
vector<string> res;
string path, s;
int n;
void dfs(int u) {
if (u == n) {
res.push_back(path);
return;
}
path += s[u];
dfs(u + 1);
path.pop_back();
if (!isdigit(s[u])) {
// 如果是大写就转小写 如果是小写就转大写
path += (s[u] >= 'a' && s[u] <= 'z') ? (char)(s[u] - 'a' + 'A') : (char)(s[u] - 'A' + 'a');
dfs(u + 1);
path.pop_back();
}
}
vector<string> letterCasePermutation(string _s) {
s = _s;
n = s.size();
dfs(0);
return res;
}
};
class Solution {
List<String> res;
String path;
String s;
int n;
void dfs(int u) {
if (u == n) {
res.add(new String(path));
return;
}
path += s.charAt(u);
dfs(u + 1);
path = path.substring(0, path.length() - 1);
if (!(s.charAt(u) <= '9' && s.charAt(u) >= '0')) {
path += (s.charAt(u) >= 'a' && s.charAt(u) <= 'z') ? (char)(s.charAt(u) - 'a' + 'A') : (char)(s.charAt(u)- 'A' + 'a');
dfs(u + 1);
path = path.substring(0, path.length() - 1);
}
}
public List<String> letterCasePermutation(String _s) {
s = _s;
n = s.length();
res = new ArrayList<>();
path = new String();
dfs(0);
return res;
}
}