文章目录

  • 1 题目理解
  • 2 回溯

1 题目理解

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. You can return the output in any order.
输入:字符串
输出:字符串可能的变形
规则:每一个位置上的字符都可能变为大写或者小写。非字母的保持原字母。

Example 1:

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

2 回溯

每个字符位,可能是大写,也可能是小写。回溯递归即可实现。

class Solution {private String S;private List<String> answer;public List<String> letterCasePermutation(String S) {this.S = S;answer = new ArrayList<String>();dfs(0,"");return answer;}private void dfs(int index,String str){if(index >= S.length()){answer.add(str);}else{char ch =  S.charAt(index);if(ch>='0' && ch<='9'){dfs(index+1,str+ch);}else if(ch>='A' && ch<='Z'){dfs(index+1,str+ch);ch += 32;dfs(index+1,str+ch);}else if(ch>='a' && ch<='z'){dfs(index+1,str+ch);ch -= 32;dfs(index+1,str+ch);}}}
}

第二种方式:可以使用二进制位。对于每一位有两种选择的情况可以用二进制位来解决。

class Solution {public List<String> letterCasePermutation(String S) {int charCount = 0;for(char ch : S.toCharArray()){if(Character.isLetter(ch)){charCount++;}}List<String> answer = new ArrayList<String>();int max = (1<<charCount)-1;for(int i = 0;i<=max;i++){int j = 0;StringBuilder s = new StringBuilder();for(char ch : S.toCharArray()){if(Character.isLetter(ch)){if(((i>>j) &1)==1){s.append(Character.toLowerCase(ch));}else{s.append(Character.toUpperCase(ch));}j++;}else{s.append(ch);}}answer.add(s.toString());}return answer;}
}

此处用StringBuilder比String快了不好。

784. Letter Case Permutation相关推荐

  1. Leetcode PHP题解--D70 784. Letter Case Permutation

    2019独角兽企业重金招聘Python工程师标准>>> D70 784. Letter Case Permutation 题目链接 784. Letter Case Permutat ...

  2. leetcode算法题--Letter Case Permutation

    原题链接:https://leetcode.com/problems/letter-case-permutation/ class Solution {public:vector<string& ...

  3. Python JAVA Solutions for Leetcode

    Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...

  4. LeetCode记录总结

    LeetCode记录总结 本文章主要记录LeetCode刷题学到的知识 242.Valid Anagram 题目: Given two strings s and t , write a functi ...

  5. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  7. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  8. PAT 甲级-入门模拟

    阅读原文 当时准备 PAT 竞赛时候,买了本<算法笔记>,书中将题型进行分类,是我最系统的一次算法学习,对题型判断.解题思路都有了新的认知,本篇文章主要记录当时刷的入门模拟题,算是比较简单 ...

  9. C语言100个经典的算法

    POJ上做做ACM的题 语言的学习基础,100个经典的算法 C语言的学习要从基础开始,这里是100个经典的算法-1C语言的学习要从基础开始,这里是100个经典的算法 题目:古典问题:有一对兔子,从出生 ...

最新文章

  1. pandas读取多个文件内容为dataframe、并合并为一个dataframe、pandas创建仅有列标签而内容为空的dataframe
  2. Hyper-V应用指南之6-差异磁盘的使用[转]
  3. 计算机视觉实习岗面试准备(一.基础知识)
  4. C语言--在终端输入多行信息,找出包含“ould”的行,并打印改行
  5. 分享一篇关于饿了么的需求文档
  6. nginx mozilla_Mozilla Firefox 11岁生日快乐!
  7. mysql 列合并_实战讲解MySQL执行计划,面试官当场“要了我”
  8. JQuery简要介绍(转)
  9. 真正解决:gpg --verify sig: 无法检查签名:找不到公钥
  10. iOS IOS开发中各种型号的分辨率总结
  11. ERROR: could not access file $libdir/postgis-2.3: No such file or director解决方法
  12. mysql字符集和校对规则(character sets and collations)详解
  13. mvvm与virtual dom算法的实践——“hoz”
  14. matlab拟合公式不准确,Matlab拟合函数误差:函数值和YDATA大小不相等
  15. 爬虫逆向 js逆向常用工具简单介绍
  16. 数据结构与算法笔记:抽象思维之对比算法,发现共性(下楼梯台阶和象棋跳马问题算法重构)
  17. 运筹学 知识点总结 (七)
  18. 首师大附中科创教育平台 我的刷题记录 0313 50111117海岛帝国:诞辰之日
  19. 2018端午小长假人气榜发布:上海迪士尼蝉联景区人气榜首
  20. Window xp 桌面主题修改为windows经典后,再恢复为Window xp 的方法

热门文章

  1. session 的 源码
  2. tcp/ip四层和osi七层
  3. Xcode8更新约束
  4. Error applying BeanValidation relational constraints错误的解决
  5. java设计模式在项目中的使用_SpringMVC项目里,有必要使用一些设计模式吗?
  6. css之文本溢出处理 | 背景图片处理
  7. process.cwd __dirname __filename 区别
  8. mongodb报错 An error occurred while loading navigation: topology was destroyed
  9. Spring Cloud Gateway Predicate.Path过滤分析
  10. 后台导出大量数据超时报 nginx404错误