Java concise two-pointer solution.


  • 0
    C
    public String reverseVowels(String s) {
      Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
      char[] chars = s.toCharArray();
      int l = 0, r = chars.length-1;
      while (l < r) {
          while (l < r && !vowels.contains(Character.toLowerCase(chars[l]))) {
              l++;
          }
          while (l < r && !vowels.contains(Character.toLowerCase(chars[r]))) {
              r--;
          }
          char tmp = chars[l];
          chars[l++] = chars[r];
          chars[r--] = tmp;
      }
      return new String(chars);
    }

  • 0
    H

    This code does not capture the scenario of special characters.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.