| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.ReverseLettersThenSpecialCharactersInString; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class ReverseLettersThenSpecialCharactersInStringTwoPointers : |
| | | 16 | | IReverseLettersThenSpecialCharactersInString |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Time complexity - O(n) |
| | | 20 | | /// Space complexity - O(n) |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="s"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public string ReverseByType(string s) |
| | 5 | 25 | | { |
| | 5 | 26 | | var charArray = s.ToCharArray(); |
| | | 27 | | |
| | 5 | 28 | | var left = 0; |
| | 5 | 29 | | var right = charArray.Length - 1; |
| | | 30 | | |
| | 8 | 31 | | while (left < right) |
| | 4 | 32 | | { |
| | 14 | 33 | | while (left < right && !char.IsLetter(charArray[left])) |
| | 10 | 34 | | { |
| | 10 | 35 | | left++; |
| | 10 | 36 | | } |
| | | 37 | | |
| | 6 | 38 | | while (left < right && !char.IsLetter(charArray[right])) |
| | 2 | 39 | | { |
| | 2 | 40 | | right--; |
| | 2 | 41 | | } |
| | | 42 | | |
| | 4 | 43 | | if (left >= right) |
| | 1 | 44 | | { |
| | 1 | 45 | | break; |
| | | 46 | | } |
| | | 47 | | |
| | 3 | 48 | | (charArray[left], charArray[right]) = (charArray[right], charArray[left]); |
| | | 49 | | |
| | 3 | 50 | | left++; |
| | 3 | 51 | | right--; |
| | 3 | 52 | | } |
| | | 53 | | |
| | 5 | 54 | | left = 0; |
| | 5 | 55 | | right = charArray.Length - 1; |
| | | 56 | | |
| | 13 | 57 | | while (left < right) |
| | 8 | 58 | | { |
| | 12 | 59 | | while (left < right && char.IsLetter(charArray[left])) |
| | 4 | 60 | | { |
| | 4 | 61 | | left++; |
| | 4 | 62 | | } |
| | | 63 | | |
| | 9 | 64 | | while (left < right && char.IsLetter(charArray[right])) |
| | 1 | 65 | | { |
| | 1 | 66 | | right--; |
| | 1 | 67 | | } |
| | | 68 | | |
| | 8 | 69 | | if (left >= right) |
| | 1 | 70 | | { |
| | 1 | 71 | | continue; |
| | | 72 | | } |
| | | 73 | | |
| | 7 | 74 | | (charArray[left], charArray[right]) = (charArray[right], charArray[left]); |
| | | 75 | | |
| | 7 | 76 | | left++; |
| | 7 | 77 | | right--; |
| | 7 | 78 | | } |
| | | 79 | | |
| | 5 | 80 | | return new string(charArray); |
| | 5 | 81 | | } |
| | | 82 | | } |