Skip to content

Commit 58d56a1

Browse files
committed
feat: add rust solution to lc problem: No.3606
1 parent d3ec0b0 commit 58d56a1

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

solution/3600-3699/3606.Coupon Code Validator/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,52 @@ function validateCoupons(code: string[], businessLine: string[], isActive: boole
305305
}
306306
```
307307

308+
#### Rust
309+
310+
```rust
311+
use std::collections::HashSet;
312+
313+
impl Solution {
314+
pub fn validate_coupons(
315+
code: Vec<String>,
316+
business_line: Vec<String>,
317+
is_active: Vec<bool>,
318+
) -> Vec<String> {
319+
fn check(s: &str) -> bool {
320+
if s.is_empty() {
321+
return false;
322+
}
323+
s.chars()
324+
.all(|c| c.is_ascii_alphanumeric() || c == '_')
325+
}
326+
327+
let bs: HashSet<&str> =
328+
["electronics", "grocery", "pharmacy", "restaurant"]
329+
.iter()
330+
.copied()
331+
.collect();
332+
333+
let mut idx: Vec<usize> = Vec::new();
334+
for i in 0..code.len() {
335+
if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) {
336+
idx.push(i);
337+
}
338+
}
339+
340+
idx.sort_by(|&i, &j| {
341+
let cmp = business_line[i].cmp(&business_line[j]);
342+
if cmp == std::cmp::Ordering::Equal {
343+
code[i].cmp(&code[j])
344+
} else {
345+
cmp
346+
}
347+
});
348+
349+
idx.into_iter().map(|i| code[i].clone()).collect()
350+
}
351+
}
352+
```
353+
308354
<!-- tabs:end -->
309355

310356
<!-- solution:end -->

solution/3600-3699/3606.Coupon Code Validator/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,52 @@ function validateCoupons(code: string[], businessLine: string[], isActive: boole
298298
}
299299
```
300300

301+
#### Rust
302+
303+
```rust
304+
use std::collections::HashSet;
305+
306+
impl Solution {
307+
pub fn validate_coupons(
308+
code: Vec<String>,
309+
business_line: Vec<String>,
310+
is_active: Vec<bool>,
311+
) -> Vec<String> {
312+
fn check(s: &str) -> bool {
313+
if s.is_empty() {
314+
return false;
315+
}
316+
s.chars()
317+
.all(|c| c.is_ascii_alphanumeric() || c == '_')
318+
}
319+
320+
let bs: HashSet<&str> =
321+
["electronics", "grocery", "pharmacy", "restaurant"]
322+
.iter()
323+
.copied()
324+
.collect();
325+
326+
let mut idx: Vec<usize> = Vec::new();
327+
for i in 0..code.len() {
328+
if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) {
329+
idx.push(i);
330+
}
331+
}
332+
333+
idx.sort_by(|&i, &j| {
334+
let cmp = business_line[i].cmp(&business_line[j]);
335+
if cmp == std::cmp::Ordering::Equal {
336+
code[i].cmp(&code[j])
337+
} else {
338+
cmp
339+
}
340+
});
341+
342+
idx.into_iter().map(|i| code[i].clone()).collect()
343+
}
344+
}
345+
```
346+
301347
<!-- tabs:end -->
302348

303349
<!-- solution:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn validate_coupons(
5+
code: Vec<String>,
6+
business_line: Vec<String>,
7+
is_active: Vec<bool>,
8+
) -> Vec<String> {
9+
fn check(s: &str) -> bool {
10+
if s.is_empty() {
11+
return false;
12+
}
13+
s.chars()
14+
.all(|c| c.is_ascii_alphanumeric() || c == '_')
15+
}
16+
17+
let bs: HashSet<&str> =
18+
["electronics", "grocery", "pharmacy", "restaurant"]
19+
.iter()
20+
.copied()
21+
.collect();
22+
23+
let mut idx: Vec<usize> = Vec::new();
24+
for i in 0..code.len() {
25+
if is_active[i] && bs.contains(business_line[i].as_str()) && check(&code[i]) {
26+
idx.push(i);
27+
}
28+
}
29+
30+
idx.sort_by(|&i, &j| {
31+
let cmp = business_line[i].cmp(&business_line[j]);
32+
if cmp == std::cmp::Ordering::Equal {
33+
code[i].cmp(&code[j])
34+
} else {
35+
cmp
36+
}
37+
});
38+
39+
idx.into_iter().map(|i| code[i].clone()).collect()
40+
}
41+
}

0 commit comments

Comments
 (0)