| | 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.LexicographicallyMinimumStringAfterRemovingStars; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class LexicographicallyMinimumStringAfterRemovingStarsStackBuckets : |
| | 16 | | LexicographicallyMinimumStringAfterRemovingStarsBase |
| | 17 | | { |
| | 18 | | private const int Length = 'z' - 'a' + 1; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(n) |
| | 22 | | /// Space complexity - O(n) |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="s"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public override string ClearStars(string s) |
| 2 | 27 | | { |
| 2 | 28 | | var indexesStacks = new Stack<int>[Length]; |
| | 29 | |
|
| 108 | 30 | | for (var i = 0; i < indexesStacks.Length; i++) |
| 52 | 31 | | { |
| 52 | 32 | | indexesStacks[i] = new Stack<int>(); |
| 52 | 33 | | } |
| | 34 | |
|
| 2 | 35 | | var chars = s.ToCharArray(); |
| | 36 | |
|
| 20 | 37 | | for (var i = 0; i < chars.Length; i++) |
| 8 | 38 | | { |
| 8 | 39 | | var c = chars[i]; |
| | 40 | |
|
| 8 | 41 | | if (c == '*') |
| 1 | 42 | | { |
| 4 | 43 | | foreach (var indexesStack in indexesStacks) |
| 1 | 44 | | { |
| 1 | 45 | | if (indexesStack.Count == 0) |
| 0 | 46 | | { |
| 0 | 47 | | continue; |
| | 48 | | } |
| | 49 | |
|
| 1 | 50 | | var indexToRemove = indexesStack.Pop(); |
| | 51 | |
|
| 1 | 52 | | chars[indexToRemove] = '*'; |
| | 53 | |
|
| 1 | 54 | | break; |
| | 55 | | } |
| 1 | 56 | | } |
| | 57 | | else |
| 7 | 58 | | { |
| 7 | 59 | | indexesStacks[c - 'a'].Push(i); |
| 7 | 60 | | } |
| 8 | 61 | | } |
| | 62 | |
|
| 2 | 63 | | return BuildResult(chars); |
| 2 | 64 | | } |
| | 65 | | } |