LoginSignup
0
0

More than 3 years have passed since last update.

Leetcode #345: Reverse Vowels of a String

Last updated at Posted at 2019-08-05
func reverseVowels(_ s: String) -> String {
    let vowel: Set<Character> = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
    var start = 0
    var end = s.count - 1
    var str = Array(s)
    while start < end {
        let isStartCharVowel = vowel.contains(str[start])
        let isEndCharVowel = vowel.contains(str[end])
        if isStartCharVowel && isEndCharVowel {
            str.swapAt(start, end)
            start += 1
            end -= 1
        } else if isStartCharVowel {
            end -= 1
        } else if isEndCharVowel {
            start += 1
        } else {
            start += 1
            end -= 1
        }
    }
    return String(str)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0