Leetcode : letter-case-permutation ( 字母大小写全排列 784 )

本文的目的是收集一些典型的题目,记录其题型与解法,理解其思想,即做到一通百通!
欢迎大家提出宝贵意见!

题目

题目简介

给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。

返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。

示例 1
输入:s = "a1b2"
输出:["a1b2", "a1B2", "A1b2", "A1B2"]

示例 2:
输入: s = "3z4"
输出: ["3z4","3Z4"]

提示:
1 <= s.length <= 12
s 由小写英文字母、大写英文字母和数字组成


题解

暴力解法,直接将字母分两种情况添加,数字直接添加。
直接看代码:

class Solution {
public:
    vector<string> res;
    // 思路:大小写字符取两种情况即可;数字直接添加
    vector<string> letterCasePermutation(string s) {
        int n=s.size();
        backtrack(s,"",0,n);
        return res;
    }

    void backtrack(string s,string path,int i,int n){
        if(i==n){res.push_back(path);return;}   
        // 数字直接添加进答案
        if(s[i]>='0'&&s[i]<='9') backtrack(s,path+s[i],i+1,n);
        else{
            // 字母就原字母和转换之后的字母两种情况,然后进行下一层的枚举
            backtrack(s,path + s[i],i+1,n);
            backtrack(s,path + char(s[i]^32),i+1,n);
        }
    }
};

优化后的深度优先搜索 Java 代码:

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> letterCasePermutation(String S) {
        char[] chs = S.toCharArray();
        int n = chs.length;
        dfs(chs, n, 0);
        return res;
    }

    private void dfs(char[] chs, int n, int begin) {
        res.add(new String(chs));
        for(int i = begin; i < n; i++){
            if(!Character.isDigit(chs[i])){
                char tmp = chs[i];
                chs[i] = (char)(chs[i] - 'a' >= 0 ? chs[i] - 32 : chs[i] + 32);
                dfs(chs, n, i + 1);
                chs[i] = tmp;
            }
        }
    }
}

回溯 Java 代码:

class Solution {
    List<String> ans = new ArrayList<String>();

    public List<String> letterCasePermutation(String s) {
        StringBuilder path = new StringBuilder(s);
        dfs(s, 0, path);
        return ans;
    }

    public void dfs(String s, int pos, StringBuilder path) {
        if (pos == s.length()) {
            ans.add(path.toString());
            return;
        }

		// 是数字,就直接放到答案里,进行下一个字符的递归
        if (Character.isDigit(s.charAt(pos))) {
            path.setCharAt(pos, s.charAt(pos));
            dfs(s, pos + 1, path);
        // 是小写字符,分两种情况,直接添加进答案以及变成大写后添加进答案
        } else if (Character.isLowerCase(s.charAt(pos))) {
            path.setCharAt(pos, s.charAt(pos));
            dfs(s, pos + 1, path);
            path.setCharAt(pos, Character.toUpperCase(s.charAt(pos)));
            dfs(s, pos + 1, path);
        } else {
        	// 大写字符与小写同理
            path.setCharAt(pos, s.charAt(pos));
            dfs(s, pos + 1, path);
            path.setCharAt(pos, Character.toLowerCase(s.charAt(pos)));
            dfs(s, pos + 1, path);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZC~Reunion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值