↓Skip to main content
  1. LeetCode/

LeetCode 3606: Coupon Code Validator

·2 mins· ·
LeetCode Easy Hash-Table String Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-07-06

  • Submissions: Wrong Answer ×4 → Accepted

Approach
#

Two steps:

  1. Filter: code non-empty with only letters, digits and underscores; the category is one of the four; isActive is True.
  2. Sort: by the given category order, then by code lexicographically. Use (category order, code) as the sort key (Hash Table Template 5), with the category order from a dictionary.

My Attempts
#

VersionProblem
Attempts 1 and 2Checked characters with range(ord("a"), ord("z")), which excludes z, Z and 9. For code = ["9"] (valid) it returned nothing. Attempt 1 also did not sort
Attempt 3Fixed the ranges but sorted only by code, forgetting the category first
Attempt 4Sorted by (code, category), the wrong order. For code = ["_a", "a"] with categories ["pharmacy", "grocery"] the answer is ["a", "_a"]; both returned ["_a", "a"]
ACChanged to (category, code)

The four categories happen to be in alphabetical order, so sorting by the category string also works; a dictionary states the order without relying on that coincidence.

Solution
#

class Solution:
    def validateCoupons(self, code: List[str], businessLine: List[str], isActive: List[bool]) -> List[str]:
        order = {"electronics": 0, "grocery": 1, "pharmacy": 2, "restaurant": 3}
        valid = [
            (order[b], c)
            for c, b, active in zip(code, businessLine, isActive)
            if active and b in order and c and all(ch.isalnum() or ch == "_" for ch in c)
        ]
        return [c for _, c in sorted(valid)]

str.isalnum() also returns True for non-ASCII letters; the input here is ASCII only, so that is fine.

Complexity
#

Time O(L + n log n) with L the total code length, space O(n).

Mistakes to Avoid
#

  • range(a, b) excludes b.
  • With a multi-part sort key, the order of the parts is the priority.

Interview Explanation
#

I filter valid coupons: non-empty code of letters, digits and underscores, a listed category, and active. Then I sort by (category order, code), with the order from a dictionary. Time O(n log n).

Related

LeetCode 389: Find the Difference
·1 min
LeetCode Easy Hash-Table String Bit-Manipulation Sorting
LeetCode 389 note: t has one extra character; XOR every character of both strings, pairs cancel, and the extra one remains
LeetCode 242: Valid Anagram
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 242 note: two strings are anagrams exactly when their character counts match; compare Counters or sorted strings
LeetCode 3545: Minimum Deletions for At Most K Distinct Characters
·1 min
LeetCode Easy Greedy Hash-Table String Sorting Counting
LeetCode 3545 note: reducing the number of kinds means deleting whole kinds, starting from the least frequent
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group