题目描述
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。
示例1
输入:S = "a1b2"输出:["a1b2", "a1B2", "A1b2", "A1B2"]输入:S = "3z4"输出:["3z4", "3Z4"]输入:S = "12345"输出:["12345"]
解题思路
方法一(较麻烦)
class Solution {public:vector<string> letterCasePermutation(string s) {vector<string> ret;vector<int> location;vector<vector<char>> perm;for (int i=0; i<s.length(); i++) {if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') {location.push_back(i);char ch = s[i] <= 'Z' ? s[i] + 32 : s[i] - 32;perm.push_back(vector<char>{s[i], ch});}}queue<string> que;int idx = 0;while (idx < perm.size()) {if (que.empty()) {for (int i=0; i<2; i++) {s[location[idx]] = perm[idx][i];que.push(s);}}else {int curr_size = que.size();while (curr_size--) {string top_str = que.front();for (int i=0; i<2; i++) {top_str[location[idx]] = perm[idx][i];que.push(top_str);}que.pop();}}idx++;}if (que.empty()) {ret.push_back(s);}else {while (!que.empty()) {ret.push_back(que.front());que.pop();}}return ret;}};
