Skip to content

3934. Coupon Code Validator


3934. Coupon Code Validator

Easy


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

  • 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. code[i] is non-empty and consists only of alphanumeric characters (a-z, A-Z, 0-9) and underscores (_).
  2. businessLine[i] is one of the following four categories: "electronics", "grocery", "pharmacy", "restaurant".
  3. 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:

  • 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:

  • First coupon is inactive (invalid).
  • Second coupon is valid.
  • Third coupon has invalid business line (invalid).

 

Constraints:

  • 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.

Solution

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Solution {
    static class Pair {
        String first, second;
        public Pair(String first, String second) {
            this.first = first;
            this.second = second;
        }
        @Override
        public String toString() {
            return first + ", " + second;
        }
    }
    static class customSort implements Comparator<Pair> {
        @Override
        public int compare(Pair o1, Pair o2) {
            int op1 = o1.second.compareTo(o2.second);
            if (op1 != 0)
                return op1;
            return o1.first.compareTo(o2.first);
        }
    }
    public List<String> validateCoupons(String[] code, String[] businessLine, boolean[] isActive) {
        int n = code.length;
        List<Pair> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (check(code[i], businessLine[i], isActive[i]))
                res.add(new Pair(code[i], businessLine[i]));
        }
        Collections.sort(res, new customSort());
        List<String> ans = new ArrayList<>();
        for (int i = 0; i < res.size(); i++)
            ans.add(res.get(i).first);
        return ans;
    }
    private boolean check(String first, String second, boolean third) {
        if (third == false)
            return false;
        if (!second.equals("electronics") && !second.equals("grocery") && !second.equals("pharmacy") && !second.equals("restaurant"))
            return false;
        if (first.length() == 0)
            return false;
        for (int i = 0; i < first.length(); i++) {
            char current = first.charAt(i);
            if (!Character.isDigit(current) && !Character.isLetter(current) && current != '_')
                return false;
        }
        return true;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Explanation

[Add detailed explanation here]