| | 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.FaultyKeyboard; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class FaultyKeyboardStringBuilder : IFaultyKeyboard |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="s"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public string FinalString(string s) |
| 2 | 26 | | { |
| 2 | 27 | | var stringBuilder = new StringBuilder(); |
| | 28 | |
|
| 34 | 29 | | foreach (var c in s) |
| 14 | 30 | | { |
| 14 | 31 | | if (c == 'i') |
| 3 | 32 | | { |
| 3 | 33 | | var left = 0; |
| 3 | 34 | | var right = stringBuilder.Length - 1; |
| | 35 | |
|
| 6 | 36 | | while (left < right) |
| 3 | 37 | | { |
| 3 | 38 | | (stringBuilder[left], stringBuilder[right]) = (stringBuilder[right], stringBuilder[left]); |
| | 39 | |
|
| 3 | 40 | | left++; |
| 3 | 41 | | right--; |
| 3 | 42 | | } |
| 3 | 43 | | } |
| | 44 | | else |
| 11 | 45 | | { |
| 11 | 46 | | stringBuilder.Append(c); |
| 11 | 47 | | } |
| 14 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | return stringBuilder.ToString(); |
| 2 | 51 | | } |
| | 52 | | } |