< 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: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
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%44100%
GetBuckets()100%22100%
IsCouponValid(...)100%88100%
CollectSortedResults(...)100%44100%

File(s)

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

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 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 class CouponCodeValidatorBucketSort : ICouponCodeValidator
 16{
 117    private static readonly Dictionary<string, int> CategoryIndex = new()
 118    {
 119        ["electronics"] = 0,
 120        ["grocery"] = 1,
 121        ["pharmacy"] = 2,
 122        ["restaurant"] = 3
 123    };
 24
 25    /// <summary>
 26    ///     Time complexity - O(L + n log n), where L is the total characters in all codes
 27    ///     Space complexity - O(n)
 28    /// </summary>
 29    /// <param name="code"></param>
 30    /// <param name="businessLine"></param>
 31    /// <param name="isActive"></param>
 32    /// <returns></returns>
 33    public IList<string> ValidateCoupons(string[] code, string[] businessLine, bool[] isActive)
 234    {
 235        var buckets = GetBuckets();
 36
 1837        for (var i = 0; i < code.Length; i++)
 738        {
 739            if (!IsCouponValid(code[i], businessLine[i], isActive[i], out var category))
 440            {
 441                continue;
 42            }
 43
 344            buckets[category].Add(code[i]);
 345        }
 46
 247        return CollectSortedResults(buckets);
 248    }
 49
 50    private static List<string>[] GetBuckets()
 251    {
 252        var buckets = new List<string>[4];
 53
 2054        for (var i = 0; i < 4; i++)
 855        {
 856            buckets[i] = [];
 857        }
 58
 259        return buckets;
 260    }
 61
 62    private static bool IsCouponValid(string code, string category, bool isActive, out int categoryIndex)
 763    {
 764        categoryIndex = -1;
 65
 766        if (!isActive)
 167        {
 168            return false;
 69        }
 70
 671        if (!CategoryIndex.TryGetValue(category, out categoryIndex))
 172        {
 173            return false;
 74        }
 75
 3776        return !string.IsNullOrEmpty(code) && code.All(c => char.IsLetterOrDigit(c) || c == '_');
 777    }
 78
 79    private static List<string> CollectSortedResults(List<string>[] buckets)
 280    {
 281        var result = new List<string>();
 82
 2083        for (var i = 0; i < 4; i++)
 884        {
 885            var bucket = buckets[i];
 86
 887            if (bucket.Count == 0)
 588            {
 589                continue;
 90            }
 91
 392            bucket.Sort(StringComparer.Ordinal);
 393            result.AddRange(bucket);
 394        }
 95
 296        return result;
 297    }
 98}