Concise Java solution


  • 0
    public class Solution {
        public String reverseVowels(String s) {
            int l = 0, r = s.length() - 1;
            StringBuilder sb = new StringBuilder(s);
            String vowels = "aeiouAEIOU";
            while (l < r) {
                char lc = sb.charAt(l), rc = sb.charAt(r);
                if (vowels.indexOf(lc) != -1 && vowels.indexOf(rc) != -1) {
                    sb.setCharAt(l, rc);
                    sb.setCharAt(r, lc);
                    l++; r--;
                    continue;
                }
                if (vowels.indexOf(lc) == -1) l++;
                if (vowels.indexOf(rc) == -1) r--;
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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