-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0345-ReverseVowelsOfAString.cs
44 lines (35 loc) · 1.13 KB
/
0345-ReverseVowelsOfAString.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
44
//-----------------------------------------------------------------------------
// Runtime: 88ms
// Memory Usage: 27 MB
// Link: https://leetcode.com/submissions/detail/352774402/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0345_ReverseVowelsOfAString
{
public string ReverseVowels(string s)
{
var vowels = new HashSet<char>() { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
int i = 0, j = s.Length - 1;
var sb = new StringBuilder(s);
while (i < j)
{
while (i < s.Length && !vowels.Contains(sb[i]))
i++;
while (j >= 0 && !vowels.Contains(sb[j]))
j--;
if (i == s.Length) break;
if (j == -1) break;
if (i >= j) break;
var temp = sb[i];
sb[i] = sb[j];
sb[j] = temp;
i++;
j--;
}
return sb.ToString();
}
}
}