lintcode1032. 字母大小写转换

给定一个字符串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);
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值