| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.UsingRobotToPrintTheLexicographicallySmallestString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 7 | 27 | | { |
| 7 | 28 | | var n = input.Length; |
| | 29 | |
|
| 7 | 30 | | Span<int> charactersFrequency = stackalloc int[AlphabetLength]; |
| | 31 | |
|
| 121 | 32 | | foreach (var character in input) |
| 50 | 33 | | { |
| 50 | 34 | | charactersFrequency[CharToIndex(character)]++; |
| 50 | 35 | | } |
| | 36 | |
|
| 7 | 37 | | var minIndex = 0; |
| | 38 | |
|
| 7 | 39 | | var bufferStack = new char[n]; |
| 7 | 40 | | var bufferStackLastIndex = 0; |
| | 41 | |
|
| 7 | 42 | | var result = new char[n]; |
| 7 | 43 | | var resultLength = 0; |
| | 44 | |
|
| 121 | 45 | | foreach (var character in input) |
| 50 | 46 | | { |
| 50 | 47 | | bufferStack[bufferStackLastIndex++] = character; |
| | 48 | |
|
| 50 | 49 | | charactersFrequency[CharToIndex(character)]--; |
| | 50 | |
|
| 232 | 51 | | while (minIndex < charactersFrequency.Length && charactersFrequency[minIndex] == 0) |
| 182 | 52 | | { |
| 182 | 53 | | minIndex++; |
| 182 | 54 | | } |
| | 55 | |
|
| 100 | 56 | | while (bufferStackLastIndex > 0 && (minIndex == AlphabetLength || |
| 100 | 57 | | bufferStack[bufferStackLastIndex - 1] <= IndexToChar(minIndex))) |
| 50 | 58 | | { |
| 50 | 59 | | result[resultLength++] = bufferStack[--bufferStackLastIndex]; |
| 50 | 60 | | } |
| 50 | 61 | | } |
| | 62 | |
|
| 7 | 63 | | while (bufferStackLastIndex > 0) |
| 0 | 64 | | { |
| 0 | 65 | | result[resultLength++] = bufferStack[--bufferStackLastIndex]; |
| 0 | 66 | | } |
| | 67 | |
|
| 7 | 68 | | return new string(result); |
| 7 | 69 | | } |
| | 70 | |
|
| | 71 | | private static int CharToIndex(char c) |
| 100 | 72 | | { |
| 100 | 73 | | return c - 'a'; |
| 100 | 74 | | } |
| | 75 | |
|
| | 76 | | private static char IndexToChar(int index) |
| 66 | 77 | | { |
| 66 | 78 | | return (char)(index + 'a'); |
| 66 | 79 | | } |
| | 80 | | } |