| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.CouponCodeValidator; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class CouponCodeValidatorBucketSort : ICouponCodeValidator |
| | | 16 | | { |
| | | 17 | | private const int CategoryCount = 4; |
| | | 18 | | |
| | 1 | 19 | | private static readonly Dictionary<string, int> CategoryToIndexDictionary = new() |
| | 1 | 20 | | { |
| | 1 | 21 | | ["electronics"] = 0, |
| | 1 | 22 | | ["grocery"] = 1, |
| | 1 | 23 | | ["pharmacy"] = 2, |
| | 1 | 24 | | ["restaurant"] = 3 |
| | 1 | 25 | | }; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Time complexity - O(L + n log n), where L is the total characters in all codes |
| | | 29 | | /// Space complexity - O(n) |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="code"></param> |
| | | 32 | | /// <param name="businessLine"></param> |
| | | 33 | | /// <param name="isActive"></param> |
| | | 34 | | /// <returns></returns> |
| | | 35 | | public IList<string> ValidateCoupons(string[] code, string[] businessLine, bool[] isActive) |
| | 2 | 36 | | { |
| | 2 | 37 | | var buckets = new List<string>[CategoryCount]; |
| | | 38 | | |
| | 20 | 39 | | for (var i = 0; i < buckets.Length; i++) |
| | 8 | 40 | | { |
| | 8 | 41 | | buckets[i] = []; |
| | 8 | 42 | | } |
| | | 43 | | |
| | 18 | 44 | | for (var i = 0; i < code.Length; i++) |
| | 7 | 45 | | { |
| | 7 | 46 | | if (!IsCouponValid(code[i], businessLine[i], isActive[i])) |
| | 4 | 47 | | { |
| | 4 | 48 | | continue; |
| | | 49 | | } |
| | | 50 | | |
| | 3 | 51 | | var index = CategoryToIndexDictionary[businessLine[i]]; |
| | | 52 | | |
| | 3 | 53 | | buckets[index].Add(code[i]); |
| | 3 | 54 | | } |
| | | 55 | | |
| | 2 | 56 | | var result = new List<string>(); |
| | | 57 | | |
| | 20 | 58 | | for (var i = 0; i < buckets.Length; i++) |
| | 8 | 59 | | { |
| | 8 | 60 | | var bucket = buckets[i]; |
| | | 61 | | |
| | 8 | 62 | | if (bucket.Count == 0) |
| | 5 | 63 | | { |
| | 5 | 64 | | continue; |
| | | 65 | | } |
| | | 66 | | |
| | 3 | 67 | | bucket.Sort(StringComparer.Ordinal); |
| | | 68 | | |
| | 3 | 69 | | result.AddRange(bucket); |
| | 3 | 70 | | } |
| | | 71 | | |
| | 2 | 72 | | return result; |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | private static bool IsCouponValid(string code, string category, bool isActive) |
| | 7 | 76 | | { |
| | 7 | 77 | | if (!isActive) |
| | 1 | 78 | | { |
| | 1 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | |
| | 6 | 82 | | if (!CategoryToIndexDictionary.ContainsKey(category)) |
| | 1 | 83 | | { |
| | 1 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 5 | 87 | | if (string.IsNullOrEmpty(code)) |
| | 1 | 88 | | { |
| | 1 | 89 | | return false; |
| | | 90 | | } |
| | | 91 | | |
| | 70 | 92 | | for (var i = 0; i < code.Length; i++) |
| | 32 | 93 | | { |
| | 32 | 94 | | var c = code[i]; |
| | | 95 | | |
| | 32 | 96 | | if (char.IsLetterOrDigit(c) || c == '_') |
| | 31 | 97 | | { |
| | 31 | 98 | | continue; |
| | | 99 | | } |
| | | 100 | | |
| | 1 | 101 | | return false; |
| | | 102 | | } |
| | | 103 | | |
| | 3 | 104 | | return true; |
| | 7 | 105 | | } |
| | | 106 | | } |