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. 微信sdk 签名

    <?php namespace app\wechat\service; use think\Config; class Signature { protected $appId ; protec ...

  2. Python 中的变量还能这样理解(白话)

    一.案例分析 1.思考 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 计算软件测试大佬柠檬小姐姐,每月能存多少钱 # 坐标:深圳 # 2018年1月份 # 房租水电 4000元 # 伙食费 1000 ...

  3. sqlserver常用简单语句

    1.增 插入内容 insert into <表名> (列1,列2,列3) values ('值1','值2','值3') 检索出的内容插入到另外一张表 insert into <表名 ...

  4. Why Countries Succeed and Fail Economically

    Countries Succeed and Fail Economically(第一部分)" title="Why Countries Succeed and Fail Econo ...

  5. ZOJ 2567 Trade

    Trade Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 2567 ...

  6. oracle基于归档的增量异地恢复 --异地新增数据文件问题

    1 rac异地基于归档文件的增量恢复   2 遇到在主库新增数据文件 后 日志恢复出现路径问题 .   错误如下 : creating datafile fno=20 name=+DATA/dataf ...

  7. hdu 3943 经典数位dp好题

    /* 题意:求出p-q的第j个nya数 数位dp,求出p-q的所有nya数的个数很好求,但是询问求出最终那个第j个值时是我不会求了看了下别人的思路 具体就是把p-q的第j个转化成0-q的第low+j个 ...

  8. stl lower_bound()和up_bound()

    iter=data.erase(iter);删掉 转载:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html STL中的每个算法 ...

  9. [K/3Cloud] 创建一个业务单据表单插件

    概念 创建一个业务单据插件,处理单据的相关控制逻辑. 示例 新建一个类,继承自单据插件基类Kingdee.BOS.Core.Bill.PlugIn.AbstractBillPlugIn. using ...

  10. MVC view页面需要多个model,复杂网页的处理

    需求描述 一个比较复杂的页面,界面中包含的元素数据来自于许多个有关联或者无关联的表,然后我们要做的就是将数据呈现在界面上. 10年前大概都是这么干的 直接写一个复杂的SQL语句,返回一个包含所需数据的 ...