Leetcode 800 Solution

This article provides solution to leetcode question 800 (letter-case-permutation)

https://leetcode.com/problems/letter-case-permutation

Solution

class Solution {
    vector<string> ans;

public:
    void _letterCasePermutation(string& S, int i) {
        if (i == S.size())
        {
            ans.push_back(S);
            return;
        }

        _letterCasePermutation(S, i + 1);

        char ch = S[i];

        if (ch >= 'a' && ch <= 'z')
            S[i] = S[i] + 'A' - 'a';
        else if (ch >= 'A' && ch <= 'Z')
            S[i] = S[i] - 'A' + 'a';
        else
            return;

        _letterCasePermutation(S, i + 1);
        S[i] = ch;
    }

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