title: My program times out on large Strings, how could I make it faster?
body: <p>I am trying to reverse the vowels of a String. My program works but it is slow and times out on large Strings. How could I make it faster?
  Write a function that takes a string as input and reverse only the vowels of a string.</p>
  
  <p>Example 1: Given <code>s = "hello"</code>, <code>return "holle"</code>.</p>
  
  <p>Example 2: Given <code>s = "leetcode"</code>, <code>return "leotcede"</code>.</p>
  
  <p>Note:
  The vowels does not include the letter "y".</p>
  
  <pre><code>public class Solution {
      public String reverseVowels(String s){
  
          Set&lt;Character&gt; vowel = new HashSet&lt;&gt;();
  
          char [] sArray = s.toCharArray();
          vowel.add('a');
          vowel.add('e');
          vowel.add('o');
          vowel.add('i');
          vowel.add('u');
          vowel.add('A');
          vowel.add('E');
          vowel.add('I');
          vowel.add('O');
          vowel.add('U');
  
          Stack&lt;Character&gt; v = new Stack&lt;&gt;();
          String temp = "";
  
          int i = 0;
          for (Character c: sArray){
              if (vowel.contains(c)){
                  v.push(c);
  
              }
              i++;
          }
          for (Character c: sArray){
              if (vowel.contains(c)){
                 if (!v.empty())
                  c = v.pop();
              }
              temp+= c;
  
          }
          return temp;
  
      }
  }
  </code></pre>
  
  <p>Edit: I have changed my code to use StringBuilder and now it passes the test cases but it is still slower than 95% of Java solutions, so I will make the other changes to try to make it faster. The slowness in my program is not entirely due to not using StringBuilder. Even with that change, my program is still very slow. It's not a duplicate of other questions.</p>