784. Letter Case Permutation C++

basic backtracking

class Solution {
public:
    void backTrack(string s, string res, int i, vector<string> &ans)
{
    if (i == s.size())
    {
        ans.push_back(res);
        return;
    }
    if (!isalpha(s[i]))
        backTrack(s, res + s[i], i + 1, ans);
    else
    {
        backTrack(s, res + (char)tolower(s[i]), i + 1, ans);
        backTrack(s, res + (char)toupper(s[i]), i + 1, ans);
    }
}

vector<string> letterCasePermutation(string S)
{
    vector<string> ans;
    string res;
    backTrack(S, res, 0, ans);
    return ans;
}
};

 

猜你喜欢

转载自www.cnblogs.com/tornado549/p/10257033.html
0条评论
添加一条新回复