784. Letter Case Permutation - Easy

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.

classical dfs

dfs helper function有三个参数,其中idx记录recursion level,当递归到最后一层时,把当前string加入res中并返回

然后判断当前字符是数字还是字母:如果是数字,直接递归到下一层并返回;如果是字母,分别转换成大、小写字母,再递归

time = O(2^n), space = O(n)

class Solution {
    public List<String> letterCasePermutation(String S) {
        List<String> res = new ArrayList<>();
        if(S == null || S.length() == 0) {
            return res;
        }
        dfs(S, 0, res);
        return res;
    }

    public void dfs(String S, int idx, List<String> res) {
        if(idx == S.length()) {
            res.add(new String(S));
            return;
        }
        char[] chs = S.toCharArray();

        if(S.charAt(idx) >= ‘0‘ && S.charAt(idx) <= ‘9‘) {
            dfs(S, idx + 1, res);
            return;
        }

        chs[idx] = Character.toUpperCase(chs[idx]);
        dfs(String.valueOf(chs), idx + 1, res);

        chs[idx] = Character.toLowerCase(chs[idx]);
        dfs(String.valueOf(chs), idx + 1, res);
    }
}

or 先把string全部转换成小写字母的char array

class Solution {
    public List<String> letterCasePermutation(String S) {
        List<String> res = new ArrayList<>();
        if(S == null || S.length() == 0) {
            return res;
        }
        char[] chs = S.toLowerCase().toCharArray();
        dfs(chs, 0, res);
        return res;
    }

    public void dfs(char[] chs, int idx, List<String> res) {
        if(idx == chs.length) {
            res.add(new String(chs));
            return;
        }

        dfs(chs, idx + 1, res);

        if(Character.isLetter(chs[idx])) {
            chs[idx] = Character.toUpperCase(chs[idx]);
            dfs(chs, idx + 1, res);
            chs[idx] = Character.toLowerCase(chs[idx]);
        }
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/11217297.html

时间: 2024-11-24 03:13:59

784. Letter Case Permutation - Easy的相关文章

784. Letter Case Permutation

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",

LeetCode题解之 Letter Case Permutation

1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 1 vector<string> letterCasePermutation(string S) { 2 vector<string> res ; 3 recursion( S,res,0 ); 4 return res; 5 6 } 7 8 void recursion(string & s , vector<string> &r ,int p){ 9 if(

784. Letter Case Permutation---back tracking

题意: 字母变成大小写,数字不变Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4" Output: ["3z4", "3Z4"] Input: S = "12345" Output: ["12345&

266. Palindrome Permutation - Easy

Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: "code" Output: false Example 2: Input: "aab" Output: true Example 3: Input: "carerac" Output: true 扫一遍s,统计频率,奇数频率最多只能出现一次.用cn

C# ignoring letter case for if statement(Stackoverflow)

Question: I have this if statement: if (input == 'day')     Console.Write({0}, dayData); When the user types 'day' it should be so that the console writes the data in that array. It works fine but is there anyway to get it to work if the user types '

LeetCode-784

784. Letter Case Permutation 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: ["a

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

LeetCode--To Lower Case &amp; Remove Outermost Parentheses (Easy)

709. To Lower Case(Easy) Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here"