给定一个字符串S,我们可以将其中所有的字符任意切换大小写并得到一个新的字符串。将所有可生成的新字符串以一个列表的形式输出。
样例
样例 1:
输入: S = "a1b2"
输出: ["a1b2", "a1B2", "A1b2", "A1B2"]
样例 2:
输入: S = "3z4"
输出: ["3z4", "3Z4"]
样例 3:
输入: S = "12345"
输出: ["12345"]
注意事项
S是一个最多长度为12的字符串。
S只包含字母或者数字。
class Solution {
public:
/**
* @param S: a string
* @return: return a list of strings
*/
vector<string> letterCasePermutation(string &S) {
// write your code here
vector<string> result;
if(S.size()==0){result.push_back(S); return result;}//排除空的情况
bool judge=false;
for (auto i : S) {//排除均为数字的情况
/* code */
if(isalpha(i)) {judge=true;break;}
}
if(judge==false) {result.push_back(S); return result;}
else{
set<string> temp;
DFS(result,S,0,temp);
return result;
}
}
void DFS(vector<string> &result,string S,int index,set<string> &temp)
{
if(index==0) result.push_back(S);
if(index==S.size()) {return;}
if(islower(S[index]))//字母为小写的情况
{
S[index]=toupper(S[index]);
if(!temp.count(S))
{
result.push_back(S);
temp.insert(S);
}
DFS(result,S,index+1,temp);
S[index]=tolower(S[index]);
DFS(result,S,index+1,temp);
}
else if(isupper(S[index]))//字母为大写的情况
{
S[index]=tolower(S[index]);
if(!temp.count(S))
{
result.push_back(S);
temp.insert(S);
}
DFS(result,S,index+1,temp);
S[index]=toupper(S[index]);
DFS(result,S,index+1,temp);
}
else{//中间出现数字的情况
DFS(result,S,index+1,temp);
}
}
};