Watch 10 video solutions for Coupon Code Validator, a easy level problem involving Array, Hash Table, String. This walkthrough by codestorywithMIK has 3,169 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
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:
@ (invalid).Example 2:
Input: code = ["GROCERY15","ELECTRONICS_50","DISCOUNT10"], businessLine = ["grocery","electronics","invalid"], isActive = [false,true,true]
Output: ["ELECTRONICS_50"]
Explanation:
Constraints:
n == code.length == businessLine.length == isActive.length1 <= n <= 1000 <= code[i].length, businessLine[i].length <= 100code[i] and businessLine[i] consist of printable ASCII characters.isActive[i] is either true or false.Problem Overview: You receive a list of coupon codes and need to determine which ones are valid based on a set of rules such as formatting constraints, character checks, and duplicate detection. The goal is to process each code efficiently and return only the coupons that satisfy the validation rules.
Approach 1: Basic Simulation (O(n * k), O(1) space)
The most direct solution iterates through every coupon string and validates it character by character. You check conditions like allowed characters, length constraints, and formatting rules using simple loops. This approach treats each coupon independently and uses conditional checks to determine whether the code should be accepted or rejected. Time complexity is O(n * k) where n is the number of coupons and k is the length of each coupon string, while extra space remains O(1) because validation happens in-place without additional data structures. This works well when the rules are simple and the dataset is small.
Approach 2: Hash Table for Duplicate Detection (O(n * k), O(n) space)
If coupon validity requires detecting duplicates or repeated codes, a hash table improves efficiency. As you iterate through the list, normalize the coupon (for example by converting to lowercase or trimming whitespace) and store it in a hash set. A constant-time lookup quickly tells you if the code already appeared earlier. The validation still scans each string once, keeping time complexity at O(n * k), but space increases to O(n) for storing previously seen coupons. This pattern is common in problems involving uniqueness checks across arrays.
Approach 3: Sorting-Based Canonical Validation (O(n * k log k), O(n) space)
Some validation rules treat coupons with the same characters as equivalent regardless of order. In that case, convert each string into a canonical form by sorting its characters. Two coupons with identical sorted representations are effectively duplicates. This uses string manipulation and sorting. For each coupon you sort its characters in O(k log k), then store the canonical version in a hash set. Overall complexity becomes O(n * k log k) time and O(n) space. The benefit is simpler duplicate detection for permutation-based rules.
Recommended for interviews: Start by explaining the basic simulation since it mirrors the problem statement and demonstrates clear reasoning. Then improve it using a hash set to track duplicates or normalized forms of coupons. Interviewers usually expect the hash-table-based approach because it maintains linear processing of the input while ensuring efficient lookups.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Basic Simulation | O(n * k) | O(1) | When only format or character validation is required and no duplicate tracking is needed |
| Hash Table Validation | O(n * k) | O(n) | General case where coupon uniqueness or repeated detection is required |
| Sorting Canonical Form | O(n * k log k) | O(n) | When coupons should be treated as equivalent regardless of character order |