Problem Details
I am trying to reverse only the vowels in a given string and return the modified String.
The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both lower and upper cases, more than once.
Example 1:
Input: s = “hello” Output: “holle”
Example 2:
Input: s = “leetcode” Output: “leotcede”
Constraints:
1 <= s.length <= 3 * 105 s consist of printable ASCII characters.
My Approach
package test.test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ReverseVowel4 {
public static String reverseVowels(String s) {
Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
char[] charArray = s.toCharArray();
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (set.contains(charArray[i]) && set.contains(charArray[j])) {
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
i++;
j--;
} else if (set.contains(s.charAt(i)) && !set.contains(s.charAt(j))) {
j--;
} else if (!set.contains(s.charAt(i)) && set.contains(s.charAt(j))) {
i++;
} else {
i++;
j--;
}
}
return new String(charArray);
}
public static void main(String[] args) {
String ss = reverseVowels("aA");
System.out.println(ss);
}
}
Question
Is this the most efficient way to reverse the vowels in a string given the constraints, or is there a better approach? If there are any optimizations or alternative methods, I would appreciate any suggestions.