题目形容
这是 LeetCode 上的 784. 字母大小写全排列 ,难度为 中等。
Tag : 「DFS」、「爆搜」、「二进制枚举」
给定一个字符串 s
,通过将字符串 s
中的每个字母转变大小写,咱们能够取得一个新的字符串。
返回 所有可能失去的字符串汇合 。以 任意程序 返回输入。
示例 1:
输出:s = "a1b2"输入:["a1b2", "a1B2", "A1b2", "A1B2"]
示例 2:
输出: s = "3z4"输入: ["3z4","3Z4"]
提醒:
- $1 <= s.length <= 12$
s
由小写英文字母、大写英文字母和数字组成
DFS
数据范畴为 $12$,同时要咱们求所有具体计划,容易想到应用 DFS
进行「爆搜」。
咱们能够从前往后思考每个 $s[i]$,依据 $s[i]$ 是否为字母进行分状况探讨:
- 若 $s[i]$ 不是字母,间接保留
- 若 $s[i]$ 是字母,则有「保留原字母」或「进行大小写转换」两种决策
设计 DFS
函数为 void dfs(int idx, int n, String cur)
:其中 $n$ 固定为具体计划的长度(即原字符串长度),而 idx
和 cur
别离为以后解决到哪一个 $s[idx]$,而 cur
则是以后具体计划。
根据上述剖析可知,当 $s[idx]$ 不为字母,将其间接追加到 cur
上,并决策下一个地位 $idx + 1$;而当 $s[idx]$ 为字母时,咱们能够抉择将 $s[idx]$ 追加到 cur
上(保留原字母)或是将 $s[idx]$ 进行翻转后再追加到 cur
上(进行大小写转换)。
最初当咱们满足 idx = n
时,阐明曾经对原字符串的每一地位决策实现,将以后具体计划 cur
退出答案。
一些细节:咱们能够通过与 32
异或来进行大小写转换
Java 代码:
class Solution { char[] cs; List<String> ans = new ArrayList<>(); public List<String> letterCasePermutation(String s) { cs = s.toCharArray(); dfs(0, s.length(), new char[s.length()]); return ans; } void dfs(int idx, int n, char[] cur) { if (idx == n) { ans.add(String.valueOf(cur)); return ; } cur[idx] = cs[idx]; dfs(idx + 1, n, cur); if (Character.isLetter(cs[idx])) { cur[idx] = (char) (cs[idx] ^ 32); dfs(idx + 1, n, cur); } }}
TypeScript 代码:
function letterCasePermutation(s: string): string[] { const ans = new Array<string>() function dfs(idx: number, n: number, cur: string): void { if (idx == n) { ans.push(cur) return } dfs(idx + 1, n, cur + s[idx]) if ((s[idx] >= 'a' && s[idx] <= 'z') || (s[idx] >= 'A' && s[idx] <= 'Z')) { dfs(idx + 1, n, cur + String.fromCharCode(s.charCodeAt(idx) ^ 32)) } } dfs(0, s.length, "") return ans}
Python 代码:
class Solution: def letterCasePermutation(self, s: str) -> List[str]: ans = [] def dfs(idx, n, cur): if idx == n: ans.append(cur) return dfs(idx + 1, n, cur + s[idx]) if 'a' <= s[idx] <= 'z' or 'A' <= s[idx] <= 'Z': dfs(idx + 1, n, cur + s[idx].swapcase()) dfs(0, len(s), '') return ans
- 工夫复杂度:最坏状况下原串
s
的每一位均为字母(均有保留和转换两种决策),此时具体计划数量共 $2^n$ 种;同时每一种具体计划咱们都须要用与原串长度雷同的复杂度来进行结构。复杂度为 $O(n \times 2^n)$ - 空间复杂度:$O(n \times 2^n)$
二进制枚举
依据解法一可知,具体计划的个数与字符串 s1
存在的字母个数相干,当 s1
存在 m
个字母时,因为每个字母存在两种决策,总的计划数个数为 $2^m$ 个。
因而能够应用「二进制枚举」的形式来做:应用变量 s
代表字符串 s1
的字母翻转状态,s
的取值范畴为 [0, 1 << m)
。若 s
的第 $j$ 位为 0
代表在 s1
中第 $j$ 个字母不进行翻转,而当为 1
时则代表翻转。
每一个状态 s
对应了一个具体计划,通过枚举所有翻转状态 s
,可结构出所有具体计划。
Java 代码:
class Solution { public List<String> letterCasePermutation(String str) { List<String> ans = new ArrayList<String>(); int n = str.length(), m = 0; for (int i = 0; i < n; i++) m += Character.isLetter(str.charAt(i)) ? 1 : 0; for (int s = 0; s < (1 << m); s++) { char[] cs = str.toCharArray(); for (int i = 0, j = 0; i < n; i++) { if (!Character.isLetter(cs[i])) continue; cs[i] = ((s >> j) & 1) == 1 ? (char) (cs[i] ^ 32) : cs[i]; j++; } ans.add(String.valueOf(cs)); } return ans; }}
TypeScript 代码:
function letterCasePermutation(str: string): string[] { function isLetter(ch: string): boolean { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') } const ans = new Array<string>() let n = str.length, m = 0 for (let i = 0; i < n; i++) m += isLetter(str[i]) ? 1 : 0 for (let s = 0; s < (1 << m); s++) { let cur = '' for (let i = 0, j = 0; i < n; i++) { if (!isLetter(str[i]) || ((s >> j) & 1) == 0) cur += str[i] else cur += String.fromCharCode(str.charCodeAt(i) ^ 32) if (isLetter(str[i])) j++ } ans.push(cur) } return ans}
Python 代码:
class Solution: def letterCasePermutation(self, s1: str) -> List[str]: def isLetter(ch): return 'a' <= ch <= 'z' or 'A' <= ch <= 'Z' ans = [] n, m = len(s1), len([ch for ch in s1 if isLetter(ch)]) for s in range(1 << m): cur = '' j = 0 for i in range(n): if not isLetter(s1[i]) or not ((s >> j) & 1): cur += s1[i] else: cur += s1[i].swapcase() if isLetter(s1[i]): j += 1 ans.append(cur) return ans
- 工夫复杂度:最坏状况下原串
s
的每一位均为字母(均有保留和转换两种决策),此时具体计划数量共 $2^n$ 种;同时每一种具体计划咱们都须要用与原串长度雷同的复杂度来进行结构。复杂度为 $O(n \times 2^n)$ - 空间复杂度:$O(n \times 2^n)$
最初
这是咱们「刷穿 LeetCode」系列文章的第 No.784
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,局部是有锁题,咱们将先把所有不带锁的题目刷完。
在这个系列文章外面,除了解说解题思路以外,还会尽可能给出最为简洁的代码。如果波及通解还会相应的代码模板。
为了不便各位同学可能电脑上进行调试和提交代码,我建设了相干的仓库:https://github.com/SharingSou... 。
在仓库地址里,你能够看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其余优选题解。
更多更全更热门的「口试/面试」相干材料可拜访排版精美的 合集新基地
本文由mdnice多平台公布