LeetCode problem solution --784: capitalization full array

Related topics

Topic Link

LeetCode China, https://leetcode-cn.com/problems/letter-case-permutation/ . Note need to log in.

My OJ, http://47.110.135.197/problem.php?id=5257 .

Title Description

Given a string S, S by the string shift each letter case, we can get a new string. Returns the set of all possible strings obtained.

Input Format

Only one line, a string S.
Ensure that all input only numbers and letters.

Output Format

Several lines of a character string representing a variation S.

SAMPLE INPUT

a1b2

Sample Output

["a1b2","a1B2","A1b2","A1B2"]

Topic analysis

Analysis of the meaning of problems

Given a string s, according to the rules of all output changes. There are two rules: 1, the letters remains the same; 2, change the case of letters. A standard template DFS title, entry-level difficulty.

Sample data analysis

After getting the string s, starts from the 0th character search one by one. Assuming that the position of the letters a, where it according to the rules, we can plot the change may be as shown below:

Option One, go to the left, the letters remain unchanged and continue to search for the next character. We can see that remain the same, whether numeric or alphabetic operation is the same.

Option Two, go right, if that letter, it is sensitive to change, then continue to search for the next character.

DFS whole process, we start with a plan, and then use Option II. Here we use the sample input A1B2, painted to simulate what DFS traversal can be drawn out change process shown in the figure, the red output.

Algorithm thinking

1, reads the string s.

2, starting from zero characters, according to the rules (to use a program, and then use Option II) changed.

Search termination condition

Termination condition must be searched for the string length.

Search function parameters

Let's analyze what parameter dfs () function requires. Since the result is a string, so DFS () function,

1, need to pass a string representing the current character is kind of how.

2, but also a position that represents the current enumeration to which local string, which is the first of several strings.

So we can determine this question dfs () function prototype should be as follows:

//参数s:表示当前字符串的内容
//参数index:表示现在枚举到字符串s的第几位
void dfs(string &s, int index) {
    if (index==s.length()) {
        return;
    }
    dfs(s, index+1);//保持不变直接搜索下一位
    if (s[index]>='A') {
        //是字母
        s[index] ^= 32;//大小写切换
        dfs(s, index+1);//搜索下一位
    }
}

Start calling way

dfs(s, 0);//表示从第0位开始搜索字符串s

Backtracking

This problem does not require backtracking, search directly on it.

AC reference code

class Solution {
public:
    vector<string> ans;

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

    void dfs(string &s, int idx) {
        if (s.length()==idx) {
            ans.push_back(s);
            return;
        }

        //本位不变,搜索下一位
        dfs(s, idx+1);

        //本位变,搜索下一位
        if (s[idx]>='A') {
            s[idx] ^= 32;
            dfs(s, idx+1);
        }
    }
};

Code Guide

 Changes in capital and lowercase letters

Changes in lowercase letters, may be used directly judge judgment statement. Here I used a little trick, the current letter XOR 32. Use a bit manipulation. Let's analyze why you can do it. Character 'A' is the ASCII 65, corresponding to the binary 0b01 0 00001, while the character 'a' is the ASCII 97, corresponding to the binary 0b01 . 1 00001, compare the 'A' and 'a' is made of two value, we can find the difference of a (red), that 0b00100000, corresponding to 32 decimal. 'B' and 'b' is true, so the other.

DFS Code routine

DFS Code routines can refer to my other article, https://blog.csdn.net/justidle/article/details/104925699 .

Published 239 original articles · won praise 291 · Views 1.07 million +

Guess you like

Origin blog.csdn.net/justidle/article/details/104930114