784. Letter Case Permutation

网友投稿 610 2022-10-09

784. Letter Case Permutation

784. Letter Case Permutation

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即可,遇到数字跳过,遇到字母,分为两种情况传给子问题。

class Solution { public List letterCasePermutation(String S) { all = new ArrayList<>(); backtrack(S.toCharArray(), 0, ""); return all; } List all; public void backtrack(char[] cs, int pos, String ans) { if (pos == cs.length) { all.add(ans); return; } else { if (Character.isAlphabetic(cs[pos])) { char l = cs[pos]; backtrack(cs, pos + 1, ans + l); if (l >= 'a' && l <= 'z') l = (char) (l - 'a' + 'A'); else if (l >= 'A' && l <= 'Z') l = (char) (l - 'A' + 'a'); backtrack(cs, pos + 1, ans + l); } else { backtrack(cs, pos + 1, ans + cs[pos]); } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:朋友圈--微信小程序(类似朋友圈的小程序)
下一篇:JVM加载class文件的原理机制实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~