Reverse Vowels of a String

🏠 ⬅️ ➡️

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s = "hello" Output: "holle"

Example 2:

Input: s = "leetcode" Output: "leotcede"

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consist of printable ASCII characters.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=:), allocatable :: s
    integer :: i, n

    ! Example 1
    s = "hello"
    print '(A)', reverse_vowels(s)

    ! Example 2
    s = "leetcode"
    print '(A)', reverse_vowels(s)

contains

    function reverse_vowels(str) result(res)
        implicit none
        character(len=*), intent(in) :: str
        character(len=len(str)), allocatable :: res
        logical :: is_vowel

        allocate(character(len=len(str)) :: res)
        res = str

        do i = 1, len(str)
            if (is_vowel(res(i:i))) then
                call reverse_substring(res, i, i)
            end if
        end do

    contains

        pure logical function is_vowel(c)
            implicit none
            character, intent(in) :: c

            select case (c)
            case ('a', 'e', 'i', 'o', 'u')
                is_vowel = .true.
            case default
                is_vowel = .false.
            end select
        end function is_vowel

        subroutine reverse_substring(str, start, end)
            implicit none
            character(len=*), intent(inout) :: str
            integer, value :: start, end
            integer :: i

            do i = start, (start + end)/2
                call swap_chars(str, i, end - i + start)
            end do
        end subroutine reverse_substring

        subroutine swap_chars(str, i, j)
            implicit none
            character(len=*), intent(inout) :: str
            integer, value :: i, j
            character :: tmp

            tmp = str(i:i)
            str(i:i) = str(j:j)
            str(j:j) = tmp
        end subroutine swap_chars

    end function reverse_vowels
end program main
Compiled
Executed
Correct
module reverse_vowels

implicit none

contains

subroutine reverse_vowels(s)

character(len=*), intent(inout) :: s

! Local variables

integer :: i, j, n
character :: c

! Initialize variables

i = 1
j = len(s)
n = len(s)

! Reverse the vowels in the string

do while (i <= j)

    if (is_vowel(s(i:i))) then

        c = s(i:i)
        s(i:i) = s(j:j)
        s(j:j) = c

        i = i + 1
        j = j - 1

    else

        i = i + 1

    end if

end do

end subroutine reverse_vowels

logical function is_vowel(c)

character(len=1), intent(in) :: c

is_vowel = (c == 'a' .or. c == 'e' .or. c == 'i' .or. c == 'o' .or. c == 'u')

end function is_vowel

end module reverse_vowels

! Test the subroutine with the given examples

program test_reverse_vowels

use reverse_vowels

implicit none

! Examples

character(len=10) :: s

s = "hello"
call reverse_vowels(s)
write (*,*) s

s = "leetcode"
call reverse_vowels(s)
write (*,*) s

end program test_reverse_vowels
🌐 Data from online sources
def reverseVowels(s: str) -> str:
    vowels = set('aeiouAEIOU')
    s_list = list(s)
    i, j = 0, len(s) - 1
    while i < j:
        if s_list[i] not in vowels:
            i += 1
        elif s_list[j] not in vowels:
            j -= 1
        else:
            s_list[i], s_list[j] = s_list[j], s_list[i]
            i += 1
            j -= 1
    return ''.join(s_list)

The algorithm uses two-pointer technique to iterate over the string from both the start and the end. It then checks if the characters at the pointers are vowels or not. If the characters are not vowels, the pointers move accordingly. If both characters are vowels, they are swapped and the pointers move forward. This process continues until the pointers meet in the middle of the string or cross each other. Finally, the modified string is returned.

🌐 Data from online sources
#include <algorithm>
#include <string>

std::string reverseVowels(std::string s) {
    int i = 0, j = s.size() - 1;
    while (i < j) {
        if (!isVowel(s[i])) {
            i++;
        } else if (!isVowel(s[j])) {
            j--;
        } else {
            std::swap(s[i], s[j]);
            i++;
            j--;
        }
    }
    return s;
}

bool isVowel(char c) {
    c = std::tolower(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

The algorithm uses two-pointer technique to iterate over the string from both the start and the end. It then checks if the characters at the pointers are vowels or not. If the characters are not vowels, the pointers move accordingly. If both characters are vowels, they are swapped and the pointers move forward. This process continues until the pointers meet in the middle of the string or cross each other. Finally, the modified string is returned.