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 at most 12.
  • S will consist only of letters or digits.

本质上是排列问题,经典的dfs求解。将字符串看作一棵树,dfs遍历过程中修改node为大写或者小写字母即可!

解法1:

class Solution {
public:
vector<string> letterCasePermutation(string S) {
// A! use DFS
vector<string> ans;
dfs(ans, S, 0);
return ans;
} void dfs(vector<string> &ans, string &S, int i) {
if(i==S.size()) {
ans.push_back(string(S));
return;
}
dfs(ans, S, i+1); #本质上就是等价于tree node的dfs(root.left)
if(S[i]>='a' && S[i]<='z') { #本质上就是等价于tree node的dfs(root.right) 如果有right的话
S[i]-=32;
dfs(ans, S, i+1);
} else if (S[i]>='A' && S[i]<='Z') {
S[i]+=32;
dfs(ans, S, i+1);
}
}
};

比我精简的写法:

class Solution {
void backtrack(string &s, int i, vector<string> &res) {
if (i == s.size()) {
res.push_back(s);
return;
}
backtrack(s, i + 1, res);
if (isalpha(s[i])) {
// toggle case
s[i] ^= (1 << 5);
backtrack(s, i + 1, res);
}
}
public:
vector<string> letterCasePermutation(string S) {
vector<string> res;
backtrack(S, 0, res);
return res;
}
};

使用BFS:本质上和tree的BFS一样,只是tree的node是字符串的char。

class Solution(object):
def letterCasePermutation(self, S):
"""
:type S: str
:rtype: List[str]
"""
# A! problem, use BFS
q = [S] # tree的层序遍历也一样
for i in range(0, len(S)):
if S[i].isalpha():
q += [s[:i] + chr(ord(s[i]) ^ 32) + s[i+1:] for s in q]
return q

另外一种写法:

def letterCasePermutation(self, S):
res = ['']
for ch in S:
if ch.isalpha():
res = [i+j for i in res for j in [ch.upper(), ch.lower()]]
else:
res = [i+ch for i in res]
return res

leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码的更多相关文章

  1. LeetCode 784 Letter Case Permutation 解题报告

    题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...

  2. 【Leetcode_easy】784. Letter Case Permutation

    problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...

  3. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  4. [LeetCode&Python] Problem 784. Letter Case Permutation

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  5. 784. Letter Case Permutation 字符串中字母的大小写组合

    [抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...

  6. 784. Letter Case Permutation C++字母大小写全排列

    网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...

  7. 【easy】784. Letter Case Permutation

    Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...

  8. 784. Letter Case Permutation

    这个题的思想很重要,两种方法 第一种,回溯法 class Solution { public: int sz; vector<string> letterCasePermutation(s ...

  9. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

随机推荐

  1. CF792E Colored Balls

    题目大意:将n个数分解成若干组,如4 = 2+2, 7 = 2+2+3,保证所有组中数字之差<=1. 首先我们能想到找一个最小值x,然后从x+1到1枚举并check,找到了就输出.这是40分做法 ...

  2. [Python3网络爬虫开发实战] 3.1.3-解析链接

    前面说过,urllib库里还提供了parse这个模块,它定义了处理URL的标准接口,例如实现URL各部分的抽取.合并以及链接转换.它支持如下协议的URL处理:file.ftp.gopher.hdl.h ...

  3. 关于ie8下disabled属性:字体颜色问题

    在ie8下,input/textarea输入框如果使用disabled属性,字体的颜色会变灰,这时我们可以使用另一种方法实现它. 不使用disabled,用readonly代替: input[read ...

  4. hdu 2377 Bus Pass

    Bus Pass Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. (十三)python 3 集合

    定义: 1.不同元素组成 2.无序 3.集合中的元素必须是不可变类型 创建集合 s = {1,2,3,4,5,6,7,8} 1.定义可变集合 >>> set_test = set(' ...

  6. hadoop_exporter+prometheus

    1.准备工作 安装go.glibe(需要连google服务器,咋连的,我就不写了,因为尝试了各种办法,都失败了,很伤心) 2.下载hadoop_exporter cd /usr/local/prom/ ...

  7. Fiddler基本用法:手机抓包

    from:https://blog.csdn.net/gld824125233/article/details/52588275 电脑最好是笔记本,这样能和手机保持统一局域网内:其他不多说,直接说步骤 ...

  8. xtu summer individual 5 F - Post Office

    Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID ...

  9. NYOJ-851寻找最大数(二),栈贪心!

    寻找最大数(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给你一个数字n(可能有前缀0). 要求从高位到低位,进行 进栈出栈 操作,是最后输出的结果最大. 输入 ...

  10. 总结:常用的Linux系统监控命令(2)

    判断I/O瓶颈 mpstat命令 命令:mpstat -P ALL 1 1000 结果显示: 注意一下这里面的%iowait列,CPU等待I/O操作所花费的时间.这个值持续很高通常可能是I/O瓶颈所导 ...