| | 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 | | using System.Text; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.ConstructStringWithRepeatLimit; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class ConstructStringWithRepeatLimitGreedy : IConstructStringWithRepeatLimit |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <param name="repeatLimit"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string RepeatLimitedString(string s, int repeatLimit) |
| 2 | 27 | | { |
| 2 | 28 | | var countArray = new int['z' - 'a' + 1]; |
| | 29 | |
|
| 34 | 30 | | foreach (var c in s) |
| 14 | 31 | | { |
| 14 | 32 | | countArray[c - 'a']++; |
| 14 | 33 | | } |
| | 34 | |
|
| 2 | 35 | | var resultStringBuilder = new StringBuilder(); |
| | 36 | |
|
| 2 | 37 | | var character = 'z' - 'a'; |
| | 38 | |
|
| 58 | 39 | | while (character >= 0) |
| 57 | 40 | | { |
| 57 | 41 | | if (countArray[character] == 0) |
| 51 | 42 | | { |
| 51 | 43 | | character--; |
| | 44 | |
|
| 51 | 45 | | continue; |
| | 46 | | } |
| | 47 | |
|
| 6 | 48 | | var count = Math.Min(countArray[character], repeatLimit); |
| | 49 | |
|
| 6 | 50 | | countArray[character] -= count; |
| | 51 | |
|
| 34 | 52 | | for (var i = 0; i < count; i++) |
| 11 | 53 | | { |
| 11 | 54 | | resultStringBuilder.Append((char)('a' + character)); |
| 11 | 55 | | } |
| | 56 | |
|
| 6 | 57 | | if (count < repeatLimit || countArray[character] == 0) |
| 3 | 58 | | { |
| 3 | 59 | | continue; |
| | 60 | | } |
| | 61 | |
|
| 3 | 62 | | var nextCharacter = character - 1; |
| | 63 | |
|
| 4 | 64 | | while (nextCharacter >= 0 && countArray[nextCharacter] == 0) |
| 1 | 65 | | { |
| 1 | 66 | | nextCharacter--; |
| 1 | 67 | | } |
| | 68 | |
|
| 3 | 69 | | if (nextCharacter < 0) |
| 1 | 70 | | { |
| 1 | 71 | | break; |
| | 72 | | } |
| | 73 | |
|
| 2 | 74 | | resultStringBuilder.Append((char)('a' + nextCharacter)); |
| | 75 | |
|
| 2 | 76 | | countArray[nextCharacter]--; |
| 2 | 77 | | } |
| | 78 | |
|
| 2 | 79 | | return resultStringBuilder.ToString(); |
| 2 | 80 | | } |
| | 81 | | } |