3934. Coupon Code Validator
My accepted Python solution to LeetCode problem 3934, Coupon Code Validator, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2025-12-30.
class Solution:
def validateCoupons(self, code: List[str], businessLine: List[str], isActive: List[bool]) -> List[str]:
valid_categories = {"electronics", "grocery", "pharmacy", "restaurant"}
category_order = {"electronics": 0, "grocery": 1, "pharmacy": 2, "restaurant": 3}
valid_coupons = []
for i in range(len(code)):
# Check if active
if not isActive[i]:
continue
# Check if businessLine is valid
if businessLine[i] not in valid_categories:
continue
# Check if code is non-empty and alphanumeric with underscores
if not code[i]:
continue
is_valid_code = True
for c in code[i]:
if not (c.isalnum() or c == '_'):
is_valid_code = False
break
if is_valid_code:
valid_coupons.append((category_order[businessLine[i]], code[i]))
# Sort by category first, then by code
valid_coupons.sort()
return [coupon[1] for coupon in valid_coupons]