< Summary

Information
Class: LeetCode.Algorithms.TotalCharactersInStringAfterTransformations1.TotalCharactersInStringAfterTransformations1Counting
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TotalCharactersInStringAfterTransformations1\TotalCharactersInStringAfterTransformations1Counting.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LengthAfterTransformations(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TotalCharactersInStringAfterTransformations1\TotalCharactersInStringAfterTransformations1Counting.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.TotalCharactersInStringAfterTransformations1;
 13
 14/// <inheritdoc />
 15public class TotalCharactersInStringAfterTransformations1Counting : ITotalCharactersInStringAfterTransformations1
 16{
 17    private const int Modulo = 1_000_000_007;
 18    private const byte AlphabetLength = 'z' - 'a' + 1;
 19
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(1)
 23    /// </summary>
 24    /// <param name="input"></param>
 25    /// <param name="transformationsCount"></param>
 26    /// <returns></returns>
 27    public int LengthAfterTransformations(string input, int transformationsCount)
 328    {
 329        Span<long> frequencies = stackalloc long[AlphabetLength];
 30
 2931        foreach (var c in input)
 1032        {
 1033            frequencies[c - 'a']++;
 1034        }
 35
 936        while (transformationsCount > 0)
 637        {
 21838            for (var i = AlphabetLength - 1; i >= 0 && transformationsCount > 0; i--)
 10339            {
 10340                if (frequencies[i] > 0)
 1241                {
 1242                    var nextIndex = i + 1;
 43
 1244                    if (nextIndex >= AlphabetLength)
 545                    {
 546                        nextIndex = 0;
 547                    }
 48
 1249                    frequencies[nextIndex] = (frequencies[nextIndex] + frequencies[i]) % Modulo;
 1250                }
 51
 10352                transformationsCount--;
 10353            }
 654        }
 55
 356        long result = 0;
 57
 16558        foreach (var frequency in frequencies)
 7859        {
 7860            result = (result + frequency) % Modulo;
 7861        }
 62
 363        return (int)result;
 364    }
 65}