784. Letter Case Permutation
[思路]:
对于一个字母和数字字符串,返回大写和小写的所有可能序列;
那么我们遍历每个字符,遇到字母替换大小写:
我的想法利用队列,遍历每个字符,将每种可能存到队列中,然后一个个替换,生成所有可能的字符串;
vector<string> letterCasePermutation(string S) {
deque<string> q;
vector<string> s;
string temp;
if(S.empty()){ //如果为空,返回空字符串
s.push_back("");
return s;
}
q.push_back(S);//入队列
for(int i=0;i<S.length();i++){
int len = q.size(); //字符串可能性
for(int j=0;j<len;j++){
temp = q.front();
q.pop_front();
if(isalpha(temp[i])){ //为字符
temp[i] = toupper(temp[i]);
q.push_back(temp); //大写入队列
temp[i] = tolower(temp[i]); //小写入队列
q.push_back(temp);
}else{
q.push_back(temp); //数字,重新入队,退出循环
break;
}
}
}
for(string str : q){//存入vector
s.push_back(str);
}
return s;
}