Skip to content

Commit

Permalink
added reverseVowelsOfAString
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn251 committed Feb 14, 2017
1 parent 3c62244 commit 89b8a54
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions String/reverseVowelsOfAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Write a function that takes a string as input and reverse only the vowels of a string.

// Example 1:
// Given s = "hello", return "holle".

// Example 2:
// Given s = "leetcode", return "leotcede".

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

public class Solution {

public String reverseVowels(String s) {

if(s == null || s.length() == 0) return s;

String vowels = "aeiouAEIOU";

char[] chars = s.toCharArray();

int start = 0;
int end = s.length() - 1;

while(start < end) {

while(start < end && !vowels.contains(chars[start] + "")) {

start++;

}

while(start < end && !vowels.contains(chars[end] + "")) {

end--;

}


char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;

start++;
end--;

}

return new String(chars);

}

}

0 comments on commit 89b8a54

Please sign in to comment.