Skip to content

Commit a54f597

Browse files
chore: add LeetCode daily solution
1 parent 8150653 commit a54f597

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Coupon Code Validator (Easy)
2+
3+
**Problem ID:** 3606
4+
**Date:** 2025-12-13
5+
**Link:** https://leetcode.com/problems/coupon-code-validator/
6+
7+
## Approach
8+
9+
To solve the "Coupon Code Validator" problem, we can follow a structured approach that involves filtering and sorting the coupons based on the given criteria.
10+
11+
### Main Idea:
12+
1. **Validation**: We need to check each coupon against three conditions:
13+
- The `code` must be non-empty and contain only alphanumeric characters and underscores.
14+
- The `businessLine` must be one of the specified valid categories: "electronics", "grocery", "pharmacy", or "restaurant".
15+
- The `isActive` status must be `true`.
16+
17+
2. **Sorting**: After identifying valid coupons, we need to sort them first by their `businessLine` in a specified order and then by their `code` in lexicographical order.
18+
19+
### Steps:
20+
1. **Iterate through the coupons**: Using a loop, we can check each coupon's properties based on the three validation criteria outlined above. For this, we can use a helper function to validate the `code`.
21+
22+
2. **Store valid coupons**: For each coupon that meets all the criteria, we can store its `code` in a list.
23+
24+
3. **Sorting**: Once we have the list of valid coupon codes, we will sort this list. To facilitate the custom sorting of the `businessLine`, we can create a mapping of business categories to their respective order, which will help in sorting the coupons based on their `businessLine` first.
25+
26+
### Data Structures:
27+
- **List**: To hold the valid coupon codes.
28+
- **Dictionary**: To map the business categories to their respective sort order for efficient sorting.
29+
30+
### Complexity:
31+
- **Time Complexity**: The overall time complexity is O(n log n) due to the sorting step, where n is the number of coupons. The validation step runs in O(n), making the sorting the most time-consuming operation.
32+
- **Space Complexity**: O(n) is required to store the valid coupon codes.
33+
34+
By following this approach, we ensure that we efficiently validate and sort the coupons according to the specified requirements, leading to a clear and concise solution.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public List<String> couponCodes(String[] code, String[] businessLine, boolean[] isActive) {
5+
List<String> validCoupons = new ArrayList<>();
6+
Set<String> validBusinessLines = new HashSet<>(Arrays.asList("electronics", "grocery", "pharmacy", "restaurant"));
7+
8+
for (int i = 0; i < code.length; i++) {
9+
if (isValidCoupon(code[i], businessLine[i], isActive[i], validBusinessLines)) {
10+
validCoupons.add(code[i]);
11+
}
12+
}
13+
14+
validCoupons.sort(Comparator.comparingInt(this::getBusinessLineOrder).thenComparing(String::compareTo));
15+
return validCoupons;
16+
}
17+
18+
private boolean isValidCoupon(String code, String businessLine, boolean isActive, Set<String> validBusinessLines) {
19+
return !code.isEmpty() && code.matches("[a-zA-Z0-9_]+") && validBusinessLines.contains(businessLine) && isActive;
20+
}
21+
22+
private int getBusinessLineOrder(String code) {
23+
// This method is a placeholder to ensure sorting by business line
24+
// In a real implementation, we would map business lines to their respective order
25+
return 0; // Replace with actual mapping logic if needed
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function couponCodeValidator(code, businessLine, isActive) {
2+
const validBusinessLines = ["electronics", "grocery", "pharmacy", "restaurant"];
3+
const validCoupons = [];
4+
5+
for (let i = 0; i < code.length; i++) {
6+
const c = code[i];
7+
const bl = businessLine[i];
8+
const active = isActive[i];
9+
10+
const isCodeValid = c.length > 0 && /^[a-zA-Z0-9_]+$/.test(c);
11+
const isBusinessLineValid = validBusinessLines.includes(bl);
12+
13+
if (isCodeValid && isBusinessLineValid && active) {
14+
validCoupons.push({ code: c, businessLine: bl });
15+
}
16+
}
17+
18+
validCoupons.sort((a, b) => {
19+
const businessLineOrder = validBusinessLines.indexOf(a.businessLine) - validBusinessLines.indexOf(b.businessLine);
20+
if (businessLineOrder !== 0) return businessLineOrder;
21+
return a.code.localeCompare(b.code);
22+
});
23+
24+
return validCoupons.map(coupon => coupon.code);
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def couponCodes(self, code: List[str], businessLine: List[str], isActive: List[bool]) -> List[str]:
3+
valid_business_lines = {"electronics": 0, "grocery": 1, "pharmacy": 2, "restaurant": 3}
4+
5+
valid_coupons = []
6+
7+
for c, bl, active in zip(code, businessLine, isActive):
8+
if (c and c.replace('_', '').isalnum() and
9+
bl in valid_business_lines and
10+
active):
11+
valid_coupons.append((bl, c))
12+
13+
valid_coupons.sort(key=lambda x: (valid_business_lines[x[0]], x[1]))
14+
15+
return [c for _, c in valid_coupons]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
303303
- 2025-12-10 — [Count the Number of Computer Unlocking Permutations](https://leetcode.com/problems/count-the-number-of-computer-unlocking-permutations/) (Medium) → `Medium/2025-12-10-3577-Count-the-Number-of-Computer-Unlocking-Permutations`
304304
- 2025-12-11 — [Count Covered Buildings](https://leetcode.com/problems/count-covered-buildings/) (Medium) → `Medium/2025-12-11-3531-Count-Covered-Buildings`
305305
- 2025-12-12 — [Count Mentions Per User](https://leetcode.com/problems/count-mentions-per-user/) (Medium) → `Medium/2025-12-12-3433-Count-Mentions-Per-User`
306+
- 2025-12-13 — [Coupon Code Validator](https://leetcode.com/problems/coupon-code-validator/) (Easy) → `Easy/2025-12-13-3606-Coupon-Code-Validator`

0 commit comments

Comments
 (0)