61
61
62
62
转变大小写的方法可以使用位运算实现。对于一个字母,小写形式与大写形式的 ASCII 码之差为 $32$,因此,我们可以通过将该字母的 ASCII 码与 $32$ 进行异或运算来实现大小写转换。
63
63
64
- 时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
64
+ 时间复杂度 $O(n \times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
65
65
66
66
<!-- tabs:start -->
67
67
70
70
``` python
71
71
class Solution :
72
72
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))
76
76
return
77
77
dfs(i + 1 )
78
78
if t[i].isalpha():
@@ -100,11 +100,11 @@ class Solution {
100
100
101
101
private void dfs (int i ) {
102
102
if (i >= t. length) {
103
- ans. add(String . valueOf (t));
103
+ ans. add(new String (t));
104
104
return ;
105
105
}
106
106
dfs(i + 1 );
107
- if (t[i] >= ' A ' ) {
107
+ if (Character . isLetter( t[i]) ) {
108
108
t[i] ^ = 32 ;
109
109
dfs(i + 1 );
110
110
}
@@ -118,15 +118,16 @@ class Solution {
118
118
class Solution {
119
119
public:
120
120
vector<string > letterCasePermutation(string s) {
121
+ string t = s;
121
122
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 );
125
126
return;
126
127
}
127
128
dfs(i + 1);
128
- if (s [ i] >= 'A' ) {
129
- s [ i] ^= 32;
129
+ if (isalpha(t [ i] ) ) {
130
+ t [ i] ^= 32;
130
131
dfs(i + 1);
131
132
}
132
133
};
@@ -163,46 +164,45 @@ func letterCasePermutation(s string) (ans []string) {
163
164
164
165
``` ts
165
166
function 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 [] = [];
169
169
const dfs = (i : number ) => {
170
- if (i === n ) {
171
- res .push (cs .join (' ' ));
170
+ if (i >= t . length ) {
171
+ ans .push (t .join (' ' ));
172
172
return ;
173
173
}
174
174
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 );
177
177
dfs (i + 1 );
178
178
}
179
179
};
180
180
dfs (0 );
181
- return res ;
181
+ return ans ;
182
182
}
183
183
```
184
184
185
185
#### Rust
186
186
187
187
``` rust
188
188
impl 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
+ }
198
200
}
199
- }
200
201
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
206
206
}
207
207
}
208
208
```
@@ -221,7 +221,7 @@ impl Solution {
221
221
222
222
具体地,我们可以使用一个变量 $i$ 表示当前枚举到的二进制数,其中 $i$ 的第 $j$ 位表示第 $j$ 个字母的转换方案。即 $i$ 的第 $j$ 位为 $1$ 表示第 $j$ 个字母转换为小写,而 $i$ 的第 $j$ 位为 $0$ 表示第 $j$ 个字母转换为大写。
223
223
224
- 时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
224
+ 时间复杂度 $O(n \times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
225
225
226
226
<!-- tabs: start -->
227
227
@@ -279,9 +279,7 @@ class Solution {
279
279
class Solution {
280
280
public:
281
281
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); });
285
283
vector<string > ans;
286
284
for (int i = 0; i < 1 << n; ++i) {
287
285
int j = 0;
@@ -330,6 +328,58 @@ func letterCasePermutation(s string) (ans []string) {
330
328
}
331
329
```
332
330
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
+
333
383
<!-- tabs: end -->
334
384
335
385
<!-- solution: end -->
0 commit comments