MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / letter_case_permutation

Function letter_case_permutation

784-letter-case-permutation.rs:2–26  ·  view source on GitHub ↗

动态规划 回溯

(s: String)

Source from the content-addressed store, hash-verified

1// 动态规划 回溯
2pub 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
28fn main() {
29 let s = "a1b2".to_string();

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected