LeetCode Entry
3606. Coupon Code Validator
Filter a,b,c accroding to rules
3606. Coupon Code Validator easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1203
Problem TLDR
Filter a,b,c accroding to rules #easy
Intuition
Just read the rules.
Approach
- some rules can be hacked around
- the regex in koglin faster than in rust
Complexity
-
Time complexity: \(O(nlog(n))\)
-
Space complexity: \(O(n)\)
Code
// 57ms
fun validateCoupons(c: Array<String>, b: Array<String>, a: BooleanArray) =
c.indices.filter { a[it] && Regex("\\w+") matches c[it] && b[it][0] in "egpr" }
.sortedBy { b[it][0] + c[it] }.map { c[it] }
// 0ms
pub fn validate_coupons(c: Vec<String>, b: Vec<String>, a: Vec<bool>) -> Vec<String> {
b.iter().map(|b|b.as_bytes()[0]).zip(c).zip(a).filter(|((b,c),a)|
*a && b"egrp".contains(b) && c != "" && c.chars().all(|c|c.is_alphanumeric() || c == '_'))
.sorted().map(|((_,c),_)|c).collect()
}