Leetcode / reverse-vowels-of-a-string
Pick a programming language:
Here is the source code for the solution to this problem.
public class Solution {
bool IsVowel(char c)
{
// or you could do
// return "aAeEiIoOuU".Contains(c);
return c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U';
}
public string ReverseVowels(string s) {
int i = 0;
int j = s.Length - 1;
char[] chars = s.ToCharArray();
while (i < j)
{
if (!IsVowel(chars[i]))
{
i++;
}
else if (!IsVowel(chars[j]))
{
j--;
}
else if (IsVowel(chars[i]) && IsVowel(chars[j]))
{
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
}
return new string(chars);
}
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: