< Summary

Information
Class: LeetCode.Algorithms.NumberOfWonderfulSubstrings.NumberOfWonderfulSubstringsDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfWonderfulSubstrings\NumberOfWonderfulSubstringsDictionary.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 59
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WonderfulSubstrings(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfWonderfulSubstrings\NumberOfWonderfulSubstringsDictionary.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.NumberOfWonderfulSubstrings;
 13
 14/// <inheritdoc />
 15public class NumberOfWonderfulSubstringsDictionary : INumberOfWonderfulSubstrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * k), where n is the length of the string and k is the number of characters in the alp
 19    ///     (which is constant, k = 10)
 20    ///     Space complexity - O(n)
 21    /// </summary>
 22    /// <param name="word"></param>
 23    /// <returns></returns>
 24    public long WonderfulSubstrings(string word)
 325    {
 326        long count = 0;
 27
 328        var bitmask = 0;
 29
 330        var maskCount = new Dictionary<int, int> { [0] = 1 };
 31
 2732        foreach (var c in word)
 933        {
 934            bitmask ^= 1 << (c - 'a');
 35
 936            if (maskCount.TryGetValue(bitmask, out var bitmaskCount))
 237            {
 238                count += bitmaskCount;
 239            }
 40
 19841            for (var i = 0; i < 10; i++)
 9042            {
 9043                var tempMask = bitmask ^ (1 << i);
 44
 9045                if (maskCount.TryGetValue(tempMask, out var tempMaskCount))
 1146                {
 1147                    count += tempMaskCount;
 1148                }
 9049            }
 50
 951            if (!maskCount.TryAdd(bitmask, 1))
 252            {
 253                maskCount[bitmask]++;
 254            }
 955        }
 56
 357        return count;
 358    }
 59}