| | 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 LexicographicallyMinimumStringAfterRemovingStarsTwoArrays : |
| | 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 chars = s.ToCharArray(); |
| | 29 | |
|
| 2 | 30 | | var n = chars.Length; |
| | 31 | |
|
| 2 | 32 | | var latestIndexes = new int[Length]; |
| | 33 | |
|
| 108 | 34 | | for (var i = 0; i < latestIndexes.Length; i++) |
| 52 | 35 | | { |
| 52 | 36 | | latestIndexes[i] = -1; |
| 52 | 37 | | } |
| | 38 | |
|
| 2 | 39 | | var previousIndexes = new int[n]; |
| | 40 | |
|
| 2 | 41 | | var smallestIndex = Length; |
| | 42 | |
|
| 20 | 43 | | for (var index = 0; index < n; index++) |
| 8 | 44 | | { |
| 8 | 45 | | var c = chars[index]; |
| | 46 | |
|
| 8 | 47 | | if (c == '*') |
| 1 | 48 | | { |
| 1 | 49 | | var latestIndex = latestIndexes[smallestIndex]; |
| | 50 | |
|
| 1 | 51 | | latestIndexes[smallestIndex] = previousIndexes[latestIndex]; |
| | 52 | |
|
| 1 | 53 | | chars[latestIndex] = '*'; |
| | 54 | |
|
| 1 | 55 | | while (smallestIndex < Length && latestIndexes[smallestIndex] == -1) |
| 0 | 56 | | { |
| 0 | 57 | | smallestIndex++; |
| 0 | 58 | | } |
| 1 | 59 | | } |
| | 60 | | else |
| 7 | 61 | | { |
| 7 | 62 | | var letterIndex = c - 'a'; |
| | 63 | |
|
| 7 | 64 | | previousIndexes[index] = latestIndexes[letterIndex]; |
| | 65 | |
|
| 7 | 66 | | latestIndexes[letterIndex] = index; |
| | 67 | |
|
| 7 | 68 | | smallestIndex = Math.Min(smallestIndex, letterIndex); |
| 7 | 69 | | } |
| 8 | 70 | | } |
| | 71 | |
|
| 2 | 72 | | return BuildResult(chars); |
| 2 | 73 | | } |
| | 74 | | } |