345. Reverse Vowels of a String

Source code notebook Author Update time

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Input: "hello"
Output: "holle"

Example 2:

Input: "leetcode"
Output: "leotcede"

Note: The vowels does not include the letter "y".

# @lc code=start
using LeetCode

function reverse_vowels(s::String)
    in_chars = codeunits(s)[:]
    indices = Int[]
    vowels = Set(['a', 'e', 'i', 'o', 'u'])
    for i in 1:length(s)
        s[i] ∈ vowels && push!(indices, i)
    end
    reverse!(@view(in_chars[indices]))
    in_chars |> pointer |> unsafe_string
end
# @lc code=end
reverse_vowels (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.