Reverse Vowels of a String


  • 0
    U
    class Solution {
        public String reverseVowels(String s) {
            
            int l = s.length();
            char[] r = new char[l];
            ArrayList<Integer> list = new ArrayList<Integer>();
            for(int i=0;i<l;i++){
                if(s.charAt(i)!= 'a' && s.charAt(i)!= 'e' && s.charAt(i)!= 'i' && s.charAt(i)!= 'o' && s.charAt(i)!= 'u' && s.charAt(i)!= 'A' &&            s.charAt(i)!= 'E' && s.charAt(i)!= 'I' && s.charAt(i)!= 'O' && s.charAt(i)!= 'U'){ 
                    r[i] = s.charAt(i);
                }else{
                    list.add(i);
                }
            }
            System.out.print(list.size());
            for(int j=0,k=list.size()-1;j<=k;j++,k--){
                if(j==k){
                    r[list.get(j)] = s.charAt(list.get(k));
                }else{
                    r[list.get(j)] = s.charAt(list.get(k));
                    r[list.get(k)] = s.charAt(list.get(j));
                }
                
            }
            return String.valueOf(r);
        }
    }
    
    

Log in to reply
 

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