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.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
temp.f95:32:12: 32 | contains | 1 Error: CONTAINS statement at (1) is already in a contained program unit temp.f95:35:25: 35 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:36:38: 36 | character, intent(in) :: c | 1 Error: Unexpected data declaration statement at (1) temp.f95:40:25: 40 | is_vowel = .true. | 1 Error: ‘is_vowel’ at (1) is not a variable temp.f95:42:25: 42 | is_vowel = .false. | 1 Error: ‘is_vowel’ at (1) is not a variable temp.f95:44:29: 44 | end function is_vowel | 1 Error: Expected label ‘reverse_vowels’ for END FUNCTION statement at (1) temp.f95:47:25: 47 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:48:50: 48 | character(len=*), intent(inout) :: str | 1 Error: Symbol ‘str’ at (1) already has basic type of CHARACTER temp.f95:49:40: 49 | integer, value :: start, end | 1 Error: Unexpected data declaration statement at (1) temp.f95:50:24: 50 | integer :: i | 1 Error: Unexpected data declaration statement at (1) temp.f95:55:11: 55 | end subroutine reverse_substring | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:58:25: 58 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:59:50: 59 | character(len=*), intent(inout) :: str | 1 Error: Symbol ‘str’ at (1) already has basic type of CHARACTER temp.f95:60:34: 60 | integer, value :: i, j | 1 Error: Unexpected data declaration statement at (1) temp.f95:61:28: 61 | character :: tmp | 1 Error: Unexpected data declaration statement at (1) temp.f95:66:11: 66 | end subroutine swap_chars | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:38:26: 38 | select case (c) | 1 Error: Symbol ‘c’ at (1) has no IMPLICIT type temp.f95:52:38: 52 | do i = start, (start + end)/2 | 1 Error: Symbol ‘end’ at (1) has no IMPLICIT type temp.f95:64:28: 64 | str(i:i) = str(j:j) | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:52:24: 52 | do i = start, (start + end)/2 | 1 Error: Symbol ‘start’ at (1) has no IMPLICIT type; did you mean ‘str’? temp.f95:63:15: 63 | tmp = str(i:i) | 1 Error: Symbol ‘tmp’ at (1) has no IMPLICIT type temp.f95:38:25: 38 | select case (c) | 1 Error: Argument of SELECT statement at (1) cannot be UNKNOWN
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
temp.f95:7:25: 7 | subroutine reverse_vowels(s) | 1 Error: MODULE attribute of ‘reverse_vowels’ conflicts with PROCEDURE attribute at (1) temp.f95:9:36: 9 | character(len=*), intent(inout) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:18: 13 | integer :: i, j, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:14: 14 | character :: c | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:18:5: 18 | i = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:10: 19 | j = len(s) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:10: 20 | n = len(s) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:17: 24 | do while (i <= j) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:21: 26 | if (is_vowel(s(i:i))) then | 1 Error: Syntax error in argument list at (1) temp.f95:28:16: 28 | c = s(i:i) | 1 Error: Syntax error in argument list at (1) temp.f95:32:17: 32 | i = i + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:17: 33 | j = j - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:8: 35 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:37:17: 37 | i = i + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:39:7: 39 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:41:3: 41 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:3: 43 | end subroutine reverse_vowels | 1 Error: Expecting END MODULE statement at (1) temp.f95:59:5: 59 | use reverse_vowels | 1 Fatal Error: Cannot open module file ‘reverse_vowels.mod’ for reading at (1): No such file or directory compilation terminated.
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.
#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.