6161
6262转变大小写的方法可以使用位运算实现。对于一个字母,小写形式与大写形式的 ASCII 码之差为 $32$,因此,我们可以通过将该字母的 ASCII 码与 $32$ 进行异或运算来实现大小写转换。
6363
64- 时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
64+ 时间复杂度 $O(n \times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
6565
6666<!-- tabs:start -->
6767
7070``` python
7171class Solution :
7272 def letterCasePermutation (self , s : str ) -> List[str ]:
73- def dfs (i ) :
74- if i >= len (s ):
75- ans.append(' ' .join(t))
73+ def dfs (i : int ) -> None :
74+ if i >= len (t ):
75+ ans.append(" " .join(t))
7676 return
7777 dfs(i + 1 )
7878 if t[i].isalpha():
@@ -100,11 +100,11 @@ class Solution {
100100
101101 private void dfs (int i ) {
102102 if (i >= t. length) {
103- ans. add(String . valueOf (t));
103+ ans. add(new String (t));
104104 return ;
105105 }
106106 dfs(i + 1 );
107- if (t[i] >= ' A ' ) {
107+ if (Character . isLetter( t[i]) ) {
108108 t[i] ^ = 32 ;
109109 dfs(i + 1 );
110110 }
@@ -118,15 +118,16 @@ class Solution {
118118class Solution {
119119public:
120120 vector<string > letterCasePermutation(string s) {
121+ string t = s;
121122 vector<string > ans;
122- function<void(int)> dfs = [ &] (int i) {
123- if (i >= s .size()) {
124- ans.emplace_back(s );
123+ auto dfs = [ &] (this auto&& dfs, int i) -> void {
124+ if (i >= t .size()) {
125+ ans.push_back(t );
125126 return;
126127 }
127128 dfs(i + 1);
128- if (s [ i] >= 'A' ) {
129- s [ i] ^= 32;
129+ if (isalpha(t [ i] ) ) {
130+ t [ i] ^= 32;
130131 dfs(i + 1);
131132 }
132133 };
@@ -163,46 +164,45 @@ func letterCasePermutation(s string) (ans []string) {
163164
164165``` ts
165166function letterCasePermutation(s : string ): string [] {
166- const n = s .length ;
167- const cs = [... s ];
168- const res = [];
167+ const t = s .split (' ' );
168+ const ans: string [] = [];
169169 const dfs = (i : number ) => {
170- if (i === n ) {
171- res .push (cs .join (' ' ));
170+ if (i >= t . length ) {
171+ ans .push (t .join (' ' ));
172172 return ;
173173 }
174174 dfs (i + 1 );
175- if (cs [i ] >= ' A ' ) {
176- cs [i ] = String .fromCharCode (cs [i ].charCodeAt (0 ) ^ 32 );
175+ if (t [i ]. charCodeAt ( 0 ) >= 65 ) {
176+ t [i ] = String .fromCharCode (t [i ].charCodeAt (0 ) ^ 32 );
177177 dfs (i + 1 );
178178 }
179179 };
180180 dfs (0 );
181- return res ;
181+ return ans ;
182182}
183183```
184184
185185#### Rust
186186
187187``` rust
188188impl Solution {
189- fn dfs (i : usize , cs : & mut Vec <char >, res : & mut Vec <String >) {
190- if i == cs . len () {
191- res . push (cs . iter (). collect ());
192- return ;
193- }
194- Self :: dfs (i + 1 , cs , res );
195- if cs [i ] >= 'A' {
196- cs [i ] = char :: from ((cs [i ] as u8 ) ^ 32 );
197- Self :: dfs (i + 1 , cs , res );
189+ pub fn letter_case_permutation (s : String ) -> Vec <String > {
190+ fn dfs (i : usize , t : & mut Vec <char >, ans : & mut Vec <String >) {
191+ if i >= t . len () {
192+ ans . push (t . iter (). collect ());
193+ return ;
194+ }
195+ dfs (i + 1 , t , ans );
196+ if t [i ]. is_alphabetic () {
197+ t [i ] = (t [i ] as u8 ^ 32 ) as char ;
198+ dfs (i + 1 , t , ans );
199+ }
198200 }
199- }
200201
201- pub fn letter_case_permutation (s : String ) -> Vec <String > {
202- let mut res = Vec :: new ();
203- let mut cs = s . chars (). collect :: <Vec <char >>();
204- Self :: dfs (0 , & mut cs , & mut res );
205- res
202+ let mut t : Vec <char > = s . chars (). collect ();
203+ let mut ans = Vec :: new ();
204+ dfs (0 , & mut t , & mut ans );
205+ ans
206206 }
207207}
208208```
@@ -221,7 +221,7 @@ impl Solution {
221221
222222具体地,我们可以使用一个变量 $i$ 表示当前枚举到的二进制数,其中 $i$ 的第 $j$ 位表示第 $j$ 个字母的转换方案。即 $i$ 的第 $j$ 位为 $1$ 表示第 $j$ 个字母转换为小写,而 $i$ 的第 $j$ 位为 $0$ 表示第 $j$ 个字母转换为大写。
223223
224- 时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
224+ 时间复杂度 $O(n \times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
225225
226226<!-- tabs: start -->
227227
@@ -279,9 +279,7 @@ class Solution {
279279class Solution {
280280public:
281281 vector<string > letterCasePermutation(string s) {
282- int n = 0;
283- for (char c : s)
284- if (isalpha(c)) ++n;
282+ int n = count_if(s.begin(), s.end(), [ ] (char c) { return isalpha(c); });
285283 vector<string > ans;
286284 for (int i = 0; i < 1 << n; ++i) {
287285 int j = 0;
@@ -330,6 +328,58 @@ func letterCasePermutation(s string) (ans []string) {
330328}
331329```
332330
331+ #### TypeScript
332+
333+ ``` ts
334+ function letterCasePermutation(s : string ): string [] {
335+ const ans: string [] = [];
336+ const n: number = Array .from (s ).filter (c => / [a-zA-Z ] / .test (c )).length ;
337+ for (let i = 0 ; i < 1 << n ; ++ i ) {
338+ let j = 0 ;
339+ const t: string [] = [];
340+ for (let c of s ) {
341+ if (/ [a-zA-Z ] / .test (c )) {
342+ t .push ((i >> j ) & 1 ? c .toLowerCase () : c .toUpperCase ());
343+ j ++ ;
344+ } else {
345+ t .push (c );
346+ }
347+ }
348+ ans .push (t .join (' ' ));
349+ }
350+ return ans ;
351+ }
352+ ```
353+
354+ #### Rust
355+
356+ ``` rust
357+ impl Solution {
358+ pub fn letter_case_permutation (s : String ) -> Vec <String > {
359+ let n = s . chars (). filter (| & c | c . is_alphabetic ()). count ();
360+ let mut ans = Vec :: new ();
361+ for i in 0 .. (1 << n ) {
362+ let mut j = 0 ;
363+ let mut t = String :: new ();
364+ for c in s . chars () {
365+ if c . is_alphabetic () {
366+ if (i >> j ) & 1 == 1 {
367+ t . push (c . to_lowercase (). next (). unwrap ());
368+ } else {
369+ t . push (c . to_uppercase (). next (). unwrap ());
370+ }
371+ j += 1 ;
372+ } else {
373+ t . push (c );
374+ }
375+ }
376+ ans . push (t );
377+ }
378+ ans
379+ }
380+ }
381+ ```
382+
333383<!-- tabs: end -->
334384
335385<!-- solution: end -->
0 commit comments