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

S = a1bc, 画出递归数如下:如果是字母 就是一个二叉树, 如果是数字就一个节点。所以本质上是一个二叉树的back tracking.

因为不熟悉 Java 字符串处理的一些function, 一开始写了一个特别丑陋的code:

class Solution {
    public List<String> letterCasePermutation(String S) {

        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder(S);
        dfs(new StringBuilder(), sb, result,0);
        return result;

    }

    private void dfs(StringBuilder curResult, StringBuilder S, List<String> result, int index){

          if(curResult.length() == S.length()){
              result.add(curResult.toString());
              return;
          }

          char c = S.charAt(index);
          curResult.append(c);
          dfs(curResult,S,result,index+1);
          curResult.setLength(curResult.length()-1);   

          if(c>=‘a‘ && c<=‘z‘){

             char ch = change_char(c, true);
             S.setCharAt(index,ch);
             curResult.append(ch);
             dfs(curResult,S,result,index+1);
             curResult.setLength(curResult.length()-1);
             S.setCharAt(index,c);
           }  

           else if(c>=‘A‘ && c<=‘Z‘){
              char ch = change_char(c,false);
              S.setCharAt(index,ch);
              curResult.append(ch);
              dfs(curResult,S,result,index+1);
              curResult.setLength(curResult.length()-1);
              S.setCharAt(index,c);
          }
    }

    private char change_char(char c, boolean small){
        if(small){
            return (char)(c + ‘A‘-‘a‘);
        }
        else return  (char)(c + ‘a‘-‘A‘);
    }
}

用了Character 类里的function 后的code:

优化到了95%

class Solution {
    public List<String> letterCasePermutation(String S) {

        List<String> result = new ArrayList<>();
        dfs(new StringBuilder(), S, result,0);
        return result;

    }

    private void dfs(StringBuilder curResult, String S, List<String> result, int index){

          if(curResult.length() == S.length()){
              result.add(curResult.toString());
              return;
          }

          Character c = S.charAt(index); 

          if(Character.isLetter(c)){
              // left sub tree
              curResult.append(Character.toLowerCase(c));
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);

              //right sub tree
              curResult.append(Character.toUpperCase(c));
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);
          }

          else{ // is number
              curResult.append(c);
              dfs(curResult,S,result,index+1);
              curResult.deleteCharAt(curResult.length()-1);
          }
    }
}


原文地址:https://www.cnblogs.com/keepAC/p/9938463.html

时间: 2024-11-24 03:14:04

784. Letter Case Permutation---back tracking的相关文章

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

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

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(

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 [ "((()))", "(()())", "(

Golang 特性简介

by sheepbao 主要大概介绍go语言的历史和特性,简单的入门. 来历 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛,正在用c++开发一些比较繁琐但是核心的工作,主要包括庞大的分布式集群,大牛觉得很闹心,后来c++委员会来他们公司演讲,说c++将要添加大概35种新特性.这几个大牛的其中一个人,名为:Rob Pike,听后心中一万个xxx飘过,“c++特性还不够多吗?简化c++应该更有成就感吧”.于是乎,Rob Pi

Codeforces VK Cup 2015 Wild Card Round 1 (AB)

比赛链接:http://codeforces.com/contest/522 A. Reposts time limit per test:1 second memory limit per test:256 megabytes One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started