LeetCode 784. Letter Case Permutation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaohaibo_/article/details/85322342

题目链接:字母大小写全排列 - 力扣 (LeetCode)

Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.

Examples:

Input: S = "a1b2"
Output: ["a1b2", "a1B2", "A1b2", "A1B2"]

Input: S = "3z4"
Output: ["3z4", "3Z4"]

Input: S = "12345"
Output: ["12345"]

Note:

  • S will be a string with length between 1 and 12.
  • S will consist only of letters or digits.

技巧:
A ^= 32 -> a
a^= 32 -> A

class Solution {
public:
    vector<string> ans;
    vector<string> letterCasePermutation(string S) {
        dfs(S, 0);
        return ans;
    }
    void dfs(string S, int pos){
        if(pos == S.size()) {
            ans.push_back(S);
            return;
        }
        dfs(S, pos + 1);
        if(S[pos] >= 'A'){
            S[pos] ^= 32;
            dfs(S, pos + 1);
        }
    }
};

猜你喜欢

转载自blog.csdn.net/zhaohaibo_/article/details/85322342