File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 动态规划 回溯
2
+ pub fn letter_case_permutation ( s : String ) -> Vec < String > {
3
+ let mut dp: Vec < String > = vec ! [ String :: new( ) ] ;
4
+ let mut current_count = 1 ;
5
+ for c in s. chars ( ) . collect :: < Vec < char > > ( ) {
6
+ if c. is_numeric ( ) {
7
+ for i in dp. len ( ) -current_count..dp. len ( ) {
8
+ let mut tmp = dp[ i] . clone ( ) ;
9
+ tmp. push ( c) ;
10
+ dp. push ( tmp) ;
11
+ }
12
+ } else {
13
+ for i in dp. len ( ) -current_count..dp. len ( ) {
14
+ let mut tmp_lower = dp[ i] . clone ( ) ;
15
+ let mut tmp_upper = dp[ i] . clone ( ) ;
16
+ tmp_lower. push ( c. to_ascii_lowercase ( ) ) ;
17
+ tmp_upper. push ( c. to_ascii_uppercase ( ) ) ;
18
+ dp. push ( tmp_lower) ;
19
+ dp. push ( tmp_upper) ;
20
+ }
21
+ }
22
+ current_count *= 2 ;
23
+ }
24
+ dp. retain ( |e|e. len ( ) == s. len ( ) ) ;
25
+ dp
26
+ }
27
+
28
+ fn main ( ) {
29
+ let s = "a1b2" . to_string ( ) ;
30
+ println ! ( "{:?}" , letter_case_permutation( s) ) ;
31
+ }
You can’t perform that action at this time.
0 commit comments