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)
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme