-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseVowels.cs
43 lines (35 loc) · 1.07 KB
/
ReverseVowels.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//https://leetcode.com/problems/reverse-vowels-of-a-string/
namespace LeetCode.Problems;
public sealed class ReverseVowels : ProblemBase
{
[Theory]
[ClassData(typeof(ReverseVowels))]
public override void Test(object[] data) => base.Test(data);
protected override void AddTestCases()
=> Add(it => it.Param("hello").Result("holle"))
.Add(it => it.Param("leetcode").Result("leotcede"));
private string Solution(string s)
{
var map = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
var result = s.ToArray();
var left = 0;
var right = s.Length - 1;
while (left < right)
{
if (map.Contains(s[left]))
{
if (map.Contains(s[right]))
{
(result[left], result[right]) = (result[right], result[left]);
left++;
}
right--;
}
else
{
left++;
}
}
return new string(result);
}
}