< Summary

Information
Class: LeetCode.Algorithms.CouponCodeValidator.CouponCodeValidatorBucketSort
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CouponCodeValidator\CouponCodeValidatorBucketSort.cs
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 106
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
ValidateCoupons(...)100%1010100%
IsCouponValid(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CouponCodeValidator\CouponCodeValidatorBucketSort.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.CouponCodeValidator;
 13
 14/// <inheritdoc />
 15public sealed class CouponCodeValidatorBucketSort : ICouponCodeValidator
 16{
 17    private const int CategoryCount = 4;
 18
 119    private static readonly Dictionary<string, int> CategoryToIndexDictionary = new()
 120    {
 121        ["electronics"] = 0,
 122        ["grocery"] = 1,
 123        ["pharmacy"] = 2,
 124        ["restaurant"] = 3
 125    };
 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)
 236    {
 237        var buckets = new List<string>[CategoryCount];
 38
 2039        for (var i = 0; i < buckets.Length; i++)
 840        {
 841            buckets[i] = [];
 842        }
 43
 1844        for (var i = 0; i < code.Length; i++)
 745        {
 746            if (!IsCouponValid(code[i], businessLine[i], isActive[i]))
 447            {
 448                continue;
 49            }
 50
 351            var index = CategoryToIndexDictionary[businessLine[i]];
 52
 353            buckets[index].Add(code[i]);
 354        }
 55
 256        var result = new List<string>();
 57
 2058        for (var i = 0; i < buckets.Length; i++)
 859        {
 860            var bucket = buckets[i];
 61
 862            if (bucket.Count == 0)
 563            {
 564                continue;
 65            }
 66
 367            bucket.Sort(StringComparer.Ordinal);
 68
 369            result.AddRange(bucket);
 370        }
 71
 272        return result;
 273    }
 74
 75    private static bool IsCouponValid(string code, string category, bool isActive)
 776    {
 777        if (!isActive)
 178        {
 179            return false;
 80        }
 81
 682        if (!CategoryToIndexDictionary.ContainsKey(category))
 183        {
 184            return false;
 85        }
 86
 587        if (string.IsNullOrEmpty(code))
 188        {
 189            return false;
 90        }
 91
 7092        for (var i = 0; i < code.Length; i++)
 3293        {
 3294            var c = code[i];
 95
 3296            if (char.IsLetterOrDigit(c) || c == '_')
 3197            {
 3198                continue;
 99            }
 100
 1101            return false;
 102        }
 103
 3104        return true;
 7105    }
 106}