package com.fishercoder.solutions;

import java.util.HashSet;
import java.util.Set;

/**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 _345 {
	public String reverseVowels(String s) {
		StringBuilder sb = new StringBuilder(s);
		Set<Character> vowels = new HashSet();
		vowels.add('a');
		vowels.add('e');
		vowels.add('i');
		vowels.add('o');
		vowels.add('u');
		vowels.add('A');
		vowels.add('E');
		vowels.add('I');
		vowels.add('O');
		vowels.add('U');
		//use two pointers approach would be the fastest
		int i = 0;
		int j = s.length() - 1;
		while (i < j) {
			char left = s.charAt(i);
			char right = s.charAt(j);
			while (i < j && !vowels.contains(left)) {
				i++;
				left = s.charAt(i);
			}
			while (i < j && !vowels.contains(right)) {
				j--;
				right = s.charAt(j);
			}
			char temp = left;
			sb.setCharAt(i, right);
			sb.setCharAt(j, temp);
			i++;
			j--;
		}
		return sb.toString();
	}

	public static void main(String... strings) {
		_345 test = new _345();
		String s = "leetcode";
		System.out.println(test.reverseVowels(s));
	}
}

@@ -31,16 +31,42 @@
 ons;%0D%0A%0D%0A
+import java.util.Arrays;%0D%0A
 import j
@@ -112,16 +112,61 @@
 ;%0D%0A%0D%0A/**
+%0D%0A * 345. Reverse Vowels of a String%0D%0A *%0D%0A * 
 Write a 
@@ -434,16 +434,51 @@
 _345 %7B%0D%0A
+%09public static class Solution1 %7B%0D%0A%09
 %09public 
@@ -511,16 +511,17 @@
 g s) %7B%0D%0A
+%09
 %09%09String
@@ -558,16 +558,17 @@
 (s);%0D%0A%09%09
+%09
 Set%3CChar
@@ -599,212 +599,76 @@
 Set(
-);%0D%0A%09%09vowels.add('a');%0D%0A%09%09vowels.add('e');%0D%0A%09%09vowels.add('i');%0D%0A%09%09vowels.add('o');%0D%0A%09%09vowels.add('u');%0D%0A%09%09vowels.add('A');%0D%0A%09%09vowels.add('E');%0D%0A%09%09vowels.add('I');%0D%0A%09%09vowels.add('O');%0D%0A%09%09vowels.add(
+Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 
 'U')
+)
 ;%0D%0A
+%09
 %09%09//
@@ -713,24 +713,25 @@
  fastest%0D%0A%09%09
+%09
 int i = 0;%0D%0A
@@ -730,16 +730,17 @@
 i = 0;%0D%0A
+%09
 %09%09int j 
@@ -760,16 +760,17 @@
 - 1;%0D%0A%09%09
+%09
 while (i
@@ -778,16 +778,17 @@
 %3C j) %7B%0D%0A
+%09
 %09%09%09char 
@@ -807,24 +807,25 @@
 rAt(i);%0D%0A%09%09%09
+%09
 char right =
@@ -839,16 +839,17 @@
 At(j);%0D%0A
+%09
 %09%09%09while
@@ -882,24 +882,25 @@
 s(left)) %7B%0D%0A
+%09
 %09%09%09%09i++;%0D%0A%09%09
@@ -901,16 +901,17 @@
 +;%0D%0A%09%09%09%09
+%09
 left = s
@@ -926,19 +926,21 @@
 i);%0D%0A%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09while
@@ -974,24 +974,25 @@
 (right)) %7B%0D%0A
+%09
 %09%09%09%09j--;%0D%0A%09%09
@@ -993,16 +993,17 @@
 -;%0D%0A%09%09%09%09
+%09
 right = 
@@ -1016,16 +1016,17 @@
 At(j);%0D%0A
+%09
 %09%09%09%7D%0D%0A%09%09
@@ -1026,16 +1026,17 @@
 %09%09%7D%0D%0A%09%09%09
+%09
 char tem
@@ -1046,16 +1046,17 @@
  left;%0D%0A
+%09
 %09%09%09sb.se
@@ -1078,16 +1078,17 @@
 t);%0D%0A%09%09%09
+%09
 sb.setCh
@@ -1106,22 +1106,24 @@
 p);%0D%0A%09%09%09
+%09
 i++;%0D%0A
+%09
 %09%09%09j--;%0D
@@ -1123,23 +1123,25 @@
 %09%09j--;%0D%0A
+%09
 %09%09%7D%0D%0A%09%09
+%09
 return s
@@ -1160,157 +1160,10 @@
 ;%0D%0A%09
+%09
 %7D
-%0D%0A%0D%0A%09public static void main(String... strings) %7B%0D%0A%09%09_345 test = new _345();%0D%0A%09%09String s = %22leetcode%22;%0D%0A%09%09System.out.println(test.reverseVowels(s));
 %0D%0A%09%7D

refactor 345