# LeetCode-ALg-344-ReverseVowelsOfAString

## Algorithm-Easy

Posted by Jae on December 5, 2018

### 1、题目

###### Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:

Input: "hello"
Output: "holle"
Example 2:

Input: "leetcode"
Output: "leotcede"

Note:
The vowels does not include the letter "y".


### 3、实现(Java)

public String reverseVowels2(String s) {

int i = 0;
int j = s.length() - 1;
StringBuffer sb = new StringBuffer(s);
HashSet vowelSet = new HashSet();

while (i < j)
{
if (!vowelSet.contains(ch[i]))
{
i++;
continue;
}
if (!vowelSet.contains(ch[j]))
{
j--;
continue;
}
char c = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j));
sb.setCharAt(j, c);
i++;
j--;

}
return sb.toString();
}


### 4、修改后(Java)

private boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c =='o' || c =='u' || c =='A' || c=='E' || c=='I' || c=='O' || c=='U';
}

public String reverseVowels2(String s) {

int i = 0;
int j = s.length() - 1;
char[] ch = s.toCharArray();

while (i < j)
{
if (!isVowel(ch[i]))
{
i++;
continue;
}
if (!isVowel(ch[j]))
{
j--;
continue;
}

char c = ch[i];
ch[i] = ch[j];
ch[j] = c;
i++;
j--;

}
return String.valueOf(ch);
}


### 5、END

< script src = " / js / bootstrap.min.js " >