Swift


  • 0
    F
    class Solution {
        func reverseVowels(s: String) -> String {
            if s == "" { return "" }
            let vowels = ["a","e","i","o","u","A","E","I","O","U"]
            var sVowels = [Character]()
            var reversedStr = ""
            for vChar in s.characters {
                if vowels.contains(String(vChar)) {
                    sVowels.append(vChar)
                }
            }
            for char in s.characters {
                if !vowels.contains(String(char)) {
                    reversedStr = reversedStr + String(char)
                } else if vowels.contains(String(char)) {
                    reversedStr = reversedStr + String(sVowels.removeLast())
                }
            }
            
           return reversedStr
        }
    }
    

    takes 12ms.


Log in to reply
 

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