Easy c# solution with comments to understand


  • 0
    K
      public string ReverseVowels(string s) {
        
        string vowels = "AaeEoOuUiI";
        
        Stack<char> st = new Stack<char>();
        
        // Push every vowel in the string into the Stack
        for(int i = 0; i < s.Length;i++){
            
            if(vowels.IndexOf(s[i]) != -1)
                st.Push(s[i]);
        }
        
        StringBuilder result = new StringBuilder();
        // replace the vowels in the string with the stack values
        for(int i = 0; i < s.Length;i++){
            
            if(vowels.IndexOf(s[i]) != -1)
                result.Append(st.Pop());
            else
                result.Append(s[i]);
        }
        
        return result.ToString();
       }

Log in to reply
 

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