Post

3934 Coupon Code Validator

3934 Coupon Code Validator

Coupon Code Validator image

You are given three arrays of length n that describe the properties of n coupons: code, businessLine, and isActive. The ith coupon has:

1
2
3
code[i]: a **string** representing the coupon identifier.
businessLine[i]: a **string** denoting the business category of the coupon.
isActive[i]: a **boolean** indicating whether the coupon is currently active.

A coupon is considered valid if all of the following conditions hold:

1
2
3
code[i] is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (_).
businessLine[i] is one of the following four categories: "electronics", "grocery", "pharmacy", "restaurant".
isActive[i] is **true**.

Return an array of the codes of all valid coupons, sorted first by their businessLine in the order: “electronics”, “grocery”, “pharmacy”, “restaurant”, and then by code in lexicographical (ascending) order within each category.

 

Example 1:

Input: code = [“SAVE20”,””,”PHARMA5”,”SAVE@20”], businessLine = [“restaurant”,”grocery”,”pharmacy”,”restaurant”], isActive = [true,true,true,true]

Output: [“PHARMA5”,”SAVE20”]

Explanation:

1
2
3
4
First coupon is valid.
Second coupon has empty code (invalid).
Third coupon is valid.
Fourth coupon has special character @ (invalid).

Example 2:

Input: code = [“GROCERY15”,”ELECTRONICS_50”,”DISCOUNT10”], businessLine = [“grocery”,”electronics”,”invalid”], isActive = [false,true,true]

Output: [“ELECTRONICS_50”]

Explanation:

1
2
3
First coupon is inactive (invalid).
Second coupon is valid.
Third coupon has invalid business line (invalid).

 

Constraints:

1
2
3
4
5
n == code.length == businessLine.length == isActive.length
1 <= n <= 100
0 <= code[i].length, businessLine[i].length <= 100
code[i] and businessLine[i] consist of printable ASCII characters.
isActive[i] is either true or false.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

use std::iter::zip;
use std::cmp::Ordering;

impl Solution {
    pub fn validate_coupons(
        code: Vec<String>,
        business_line: Vec<String>,
        is_active: Vec<bool>,
    ) -> Vec<String> {

        fn s(a: &(String, String, bool), b: &(String, String, bool)) -> Ordering {
    let arr = ["electronics", "grocery", "pharmacy", "restaurant"];

    let m = arr.iter().position(|x| x == &a.1).unwrap_or(arr.len());
    let n = arr.iter().position(|x| x == &b.1).unwrap_or(arr.len());

    if m == n {
        return a.0.cmp(&b.0);
    }
    m.cmp(&n)
}


        let mut comb_arr: Vec<(String, String, bool)> = Vec::new();

        // build combined array
        for ((k1, k2), k3) in zip(zip(code, business_line), is_active) {
            comb_arr.push((k1, k2, k3));
        }

        // keep sorting
        comb_arr.sort_by(s);

        let mut res = Vec::new();
        let allowed = ["electronics", "grocery", "pharmacy", "restaurant"];

        // add checks here
        for k in comb_arr {
            // active check
            if !k.2 {
                continue;
            }

            // business line check
            if !allowed.contains(&k.1.as_str()) {
                continue;
            }

            // code non-empty & valid chars
            if k.0.is_empty() {
                continue;
            }

            if !k.0.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                continue;
            }

            res.push(k.0);
        }

        res
    }
}




This post is licensed under CC BY 4.0 by the author.