This is my solution to 345. Reverse Vowels of a String,but I am getting Time Limit Exceeded


  • 0
    D

    public class Solution {
    public String reverseVowels(String s) {

    	List<Character> set = new ArrayList<Character>();
    	set.add('a');
    	set.add('e');
    	set.add('i');
    	set.add('o');
    	set.add('u');
    	set.add('A');
    	set.add('E');
    	set.add('I');
    	set.add('O');
    	set.add('U');
    	
    	
    	List<Character> set2 = new ArrayList<Character>();
    	
    	for(int i = s.length()-1 ; i >= 0 ; i--){
    		System.out.println(i);
            if(set.contains(s.charAt(i))){
            	set2.add(s.charAt(i));
            }
        }
    	
    	StringBuilder sb =  new StringBuilder();
    	int count = 0;
    	for (int i = 0; i < s.length(); i++) {
    		if(set.contains(s.charAt(i))){
            	sb.append(set2.get(count));
            	count++;
            }else{
            	sb.append(s.charAt(i));
            }
    	}
    	
    	
    	return sb.toString();
    	
    
    }
    

    }


Log in to reply
 

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