diff --git a/solution/3600-3699/3606.Coupon Code Validator/README.md b/solution/3600-3699/3606.Coupon Code Validator/README.md index 4963405f2b4d4..581852318b373 100644 --- a/solution/3600-3699/3606.Coupon Code Validator/README.md +++ b/solution/3600-3699/3606.Coupon Code Validator/README.md @@ -305,6 +305,52 @@ function validateCoupons(code: string[], businessLine: string[], isActive: boole } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn validate_coupons( + code: Vec, + business_line: Vec, + is_active: Vec, + ) -> Vec { + fn check(s: &str) -> bool { + if s.is_empty() { + return false; + } + s.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_') + } + + let bs: HashSet<&str> = + ["electronics", "grocery", "pharmacy", "restaurant"] + .iter() + .copied() + .collect(); + + let mut idx: Vec = Vec::new(); + for i in 0..code.len() { + if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) { + idx.push(i); + } + } + + idx.sort_by(|&i, &j| { + let cmp = business_line[i].cmp(&business_line[j]); + if cmp == std::cmp::Ordering::Equal { + code[i].cmp(&code[j]) + } else { + cmp + } + }); + + idx.into_iter().map(|i| code[i].clone()).collect() + } +} +``` + diff --git a/solution/3600-3699/3606.Coupon Code Validator/README_EN.md b/solution/3600-3699/3606.Coupon Code Validator/README_EN.md index 4c44507179405..c5fee6bce4cca 100644 --- a/solution/3600-3699/3606.Coupon Code Validator/README_EN.md +++ b/solution/3600-3699/3606.Coupon Code Validator/README_EN.md @@ -298,6 +298,52 @@ function validateCoupons(code: string[], businessLine: string[], isActive: boole } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn validate_coupons( + code: Vec, + business_line: Vec, + is_active: Vec, + ) -> Vec { + fn check(s: &str) -> bool { + if s.is_empty() { + return false; + } + s.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_') + } + + let bs: HashSet<&str> = + ["electronics", "grocery", "pharmacy", "restaurant"] + .iter() + .copied() + .collect(); + + let mut idx: Vec = Vec::new(); + for i in 0..code.len() { + if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) { + idx.push(i); + } + } + + idx.sort_by(|&i, &j| { + let cmp = business_line[i].cmp(&business_line[j]); + if cmp == std::cmp::Ordering::Equal { + code[i].cmp(&code[j]) + } else { + cmp + } + }); + + idx.into_iter().map(|i| code[i].clone()).collect() + } +} +``` + diff --git a/solution/3600-3699/3606.Coupon Code Validator/Solution.rs b/solution/3600-3699/3606.Coupon Code Validator/Solution.rs new file mode 100644 index 0000000000000..fe78830d07b71 --- /dev/null +++ b/solution/3600-3699/3606.Coupon Code Validator/Solution.rs @@ -0,0 +1,41 @@ +use std::collections::HashSet; + +impl Solution { + pub fn validate_coupons( + code: Vec, + business_line: Vec, + is_active: Vec, + ) -> Vec { + fn check(s: &str) -> bool { + if s.is_empty() { + return false; + } + s.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_') + } + + let bs: HashSet<&str> = + ["electronics", "grocery", "pharmacy", "restaurant"] + .iter() + .copied() + .collect(); + + let mut idx: Vec = Vec::new(); + for i in 0..code.len() { + if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) { + idx.push(i); + } + } + + idx.sort_by(|&i, &j| { + let cmp = business_line[i].cmp(&business_line[j]); + if cmp == std::cmp::Ordering::Equal { + code[i].cmp(&code[j]) + } else { + cmp + } + }); + + idx.into_iter().map(|i| code[i].clone()).collect() + } +}