< Summary

Information
Class: LeetCode.Algorithms.UsingRobotToPrintTheLexicographicallySmallestString.UsingRobotToPrintTheLexicographicallySmallestStringStack
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\UsingRobotToPrintTheLexicographicallySmallestString\UsingRobotToPrintTheLexicographicallySmallestStringStack.cs
Line coverage
92%
Covered lines: 35
Uncovered lines: 3
Coverable lines: 38
Total lines: 80
Line coverage: 92.1%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RobotWithString(...)93.75%161690.62%
CharToIndex(...)100%11100%
IndexToChar(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\UsingRobotToPrintTheLexicographicallySmallestString\UsingRobotToPrintTheLexicographicallySmallestStringStack.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.UsingRobotToPrintTheLexicographicallySmallestString;
 13
 14/// <inheritdoc />
 15public class UsingRobotToPrintTheLexicographicallySmallestStringStack :
 16    IUsingRobotToPrintTheLexicographicallySmallestString
 17{
 18    private const int AlphabetLength = 'z' - 'a' + 1;
 19
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(n)
 23    /// </summary>
 24    /// <param name="input"></param>
 25    /// <returns></returns>
 26    public string RobotWithString(string input)
 727    {
 728        var n = input.Length;
 29
 730        Span<int> charactersFrequency = stackalloc int[AlphabetLength];
 31
 12132        foreach (var character in input)
 5033        {
 5034            charactersFrequency[CharToIndex(character)]++;
 5035        }
 36
 737        var minIndex = 0;
 38
 739        var bufferStack = new char[n];
 740        var bufferStackLastIndex = 0;
 41
 742        var result = new char[n];
 743        var resultLength = 0;
 44
 12145        foreach (var character in input)
 5046        {
 5047            bufferStack[bufferStackLastIndex++] = character;
 48
 5049            charactersFrequency[CharToIndex(character)]--;
 50
 23251            while (minIndex < charactersFrequency.Length && charactersFrequency[minIndex] == 0)
 18252            {
 18253                minIndex++;
 18254            }
 55
 10056            while (bufferStackLastIndex > 0 && (minIndex == AlphabetLength ||
 10057                                                bufferStack[bufferStackLastIndex - 1] <= IndexToChar(minIndex)))
 5058            {
 5059                result[resultLength++] = bufferStack[--bufferStackLastIndex];
 5060            }
 5061        }
 62
 763        while (bufferStackLastIndex > 0)
 064        {
 065            result[resultLength++] = bufferStack[--bufferStackLastIndex];
 066        }
 67
 768        return new string(result);
 769    }
 70
 71    private static int CharToIndex(char c)
 10072    {
 10073        return c - 'a';
 10074    }
 75
 76    private static char IndexToChar(int index)
 6677    {
 6678        return (char)(index + 'a');
 6679    }
 80}