| | 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.ReverseVowelsOfString; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ReverseVowelsOfStringTwoPointers : IReverseVowelsOfString |
| | 16 | | { |
| 4 | 17 | | private readonly HashSet<char> _vowelsHashSet = |
| 4 | 18 | | [ |
| 4 | 19 | | 'a', |
| 4 | 20 | | 'e', |
| 4 | 21 | | 'i', |
| 4 | 22 | | 'o', |
| 4 | 23 | | 'u', |
| 4 | 24 | | 'A', |
| 4 | 25 | | 'E', |
| 4 | 26 | | 'I', |
| 4 | 27 | | 'O', |
| 4 | 28 | | 'U' |
| 4 | 29 | | ]; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Time complexity - O(n) |
| | 33 | | /// Space complexity - O(n) |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="s"></param> |
| | 36 | | /// <returns></returns> |
| | 37 | | public string ReverseVowels(string s) |
| 4 | 38 | | { |
| 4 | 39 | | var sArray = s.ToCharArray(); |
| | 40 | |
|
| 4 | 41 | | var left = 0; |
| 4 | 42 | | var right = s.Length - 1; |
| | 43 | |
|
| 33 | 44 | | while (left < right) |
| 29 | 45 | | { |
| 29 | 46 | | if (!_vowelsHashSet.Contains(s[left])) |
| 11 | 47 | | { |
| 11 | 48 | | left++; |
| | 49 | |
|
| 11 | 50 | | continue; |
| | 51 | | } |
| | 52 | |
|
| 18 | 53 | | if (!_vowelsHashSet.Contains(s[right])) |
| 10 | 54 | | { |
| 10 | 55 | | right--; |
| | 56 | |
|
| 10 | 57 | | continue; |
| | 58 | | } |
| | 59 | |
|
| 8 | 60 | | (sArray[left], sArray[right]) = (sArray[right], sArray[left]); |
| | 61 | |
|
| 8 | 62 | | left++; |
| 8 | 63 | | right--; |
| 8 | 64 | | } |
| | 65 | |
|
| 4 | 66 | | return new string(sArray); |
| 4 | 67 | | } |
| | 68 | | } |