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. 零基础入门学习Python(9)--了不起的分支和循环3

    前言 本节继续介绍分支和循环 知识点 while循环 Python while循环与if条件分支有点类似,在条件为真的情况下,执行某一段指定的代码.不同的是只要条件为True,while循环就会一直重 ...

  2. 用ffmpeg切割音频文件

    ffmpeg -i audio.wav -f segment -segment_time -c copy audio%02d.wav "-segment_time 60" 表示每6 ...

  3. 配置python3 项目环境

    安装python3 安装仓库软件 sudo apt-get install software-properties-common python-software-properties 添加仓库 sud ...

  4. 去面试Python工程师,这几个基础问题一定要能回答,Python面试题No4

    今天的面试题以基础为主,去面试Python工程师,这几个基础问题不能答错 第1题:列表和元组有什么不同? 列表和元组是Python中最常用的两种数据结构,字典是第三种. 相同点: 都是序列 都可以存储 ...

  5. Python多线程豆瓣影评API接口爬虫

    爬虫库 使用简单的requests库,这是一个阻塞的库,速度比较慢. 解析使用XPATH表达式 总体采用类的形式 多线程 使用concurrent.future并发模块,建立线程池,把future对象 ...

  6. Linux最常用的基础命令 下篇

    Linux最常用的基础命令个人总结 shell脚本 脚本就是:写一堆指令存成一个文本,用于完成一些小任务 a="123" linux中定义一个变量 echo $a echo $b ...

  7. WinMain名词解析

    WinMain程序名词解析 int WINAPI WinMain(HINSTANCE hInstance ,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nS ...

  8. 集训第四周(高效算法设计)F题 (二分+贪心)

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

  9. Ajax的特点

    [传统提交方式] 客户端提交请求后,服务器会找到相应的资源进行执行.并将执行结果重新发送给客户端.客户端接收到服务器端的响应会进行重新解释并显示.此时的页面是一个全新的页面. [Ajax提交] 客户端 ...

  10. Linux下汇编语言学习笔记37 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...