| | 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 LexicographicallyMinimumStringAfterRemovingStarsPriorityQueue : |
| | 16 | | LexicographicallyMinimumStringAfterRemovingStarsBase |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n log n) |
| | 20 | | /// Space complexity - O(n) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="s"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public override string ClearStars(string s) |
| 2 | 25 | | { |
| 2 | 26 | | var charsPriorityQueue = new PriorityQueue<(char Char, int Index), (char Char, int Index)>(); |
| | 27 | |
|
| 2 | 28 | | var chars = s.ToCharArray(); |
| | 29 | |
|
| 20 | 30 | | for (var i = 0; i < chars.Length; i++) |
| 8 | 31 | | { |
| 8 | 32 | | var c = chars[i]; |
| | 33 | |
|
| 8 | 34 | | if (c == '*') |
| 1 | 35 | | { |
| 1 | 36 | | var charToRemove = charsPriorityQueue.Dequeue(); |
| | 37 | |
|
| 1 | 38 | | chars[charToRemove.Index] = '*'; |
| 1 | 39 | | } |
| | 40 | | else |
| 7 | 41 | | { |
| 7 | 42 | | charsPriorityQueue.Enqueue((c, i), (c, -i)); |
| 7 | 43 | | } |
| 8 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | return BuildResult(chars); |
| 2 | 47 | | } |
| | 48 | | } |