Remove Vowels from a String

🏠 ⬅️ ➡️

Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string.

Example 1:

Input: s = "leetcodeisacommunityforcoders " Output: "ltcdscmmntyfrcdrs "

Example 2:

Input: s = "aeiou " Output: " "

Constraints:

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.

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

    character(len=1000) :: s
    character(len=1000) :: result

    ! Example 1
    s = "leetcodeisacommunityforcoders"
    write(*,"(A)") trim(remove_vowels(s))

    ! Example 2
    s = "aeiou "
    write(*,"(A)") trim(remove_vowels(s))

contains

    function remove_vowels(str) result(new_str)
        implicit none
        character(len=*), intent(in) :: str
        character(len=len(str)) :: new_str
        integer :: i, j

        do i = 1, len(str)
            if (str(i:i) /= 'a' .and. &
                str(i:i) /= 'e' .and. &
                str(i:i) /= 'i' .and. &
                str(i:i) /= 'o' .and. &
                str(i:i) /= 'u') then
                    new_str(j:j) = str(i:i)
                    j = j + 1
            end if
        end do
    end function remove_vowels

end program main
Compiled
Executed
Correct
! Declare the input string and the output string

character(len=1000) :: s, output

! Declare the vowels to be removed

character(len=5) :: vowels = "aeiou"

! Declare the length of the input string

integer :: len_s

! Get the length of the input string

len_s = len_trim(s)

! Initialize the output string

output = ""

! Loop through each character in the input string

do i = 1, len_s

! If the current character is not a vowel, add it to the output string

if (index(vowels, s(i:i)) == 0) then
output = output // s(i:i)
endif

enddo

! Print the output string to stdout

write (*,*) output

end
🌐 Data from online sources
def is_robot_bounded(instructions: str) -> bool:
    x, y, dir = 0, 0, 0
    moves = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    for c in instructions:
        if c == 'G':
            x += moves[dir][0]
            y += moves[dir][1]
        elif c == 'L':
            dir = (dir + 3) % 4
        else:
            dir = (dir + 1) % 4

    return (x == 0 and y == 0) or dir != 0
  1. Initialize (x, y) as the robot's initial position and direction as dir (0: north, 1: east, 2: south, 3: west).
  2. Declare an array moves, which represents the movement in the x and y directions when going straight.
  3. Iterate through instructions:
    • If the instruction is 'G', update x and y based on the current direction.
    • If the instruction is 'L', update dir as (dir + 3) % 4 to turn left (counter-clockwise).
    • If the instruction is 'R', update dir as (dir + 1) % 4 to turn right (clockwise).
  4. If the robot reaches the starting position (0,0) or its direction is not north (dir != 0), then it stays in a circle. Otherwise, it doesn't.
🌐 Data from online sources
bool isRobotBounded(string instructions) {
    int x = 0, y = 0, dir = 0;
    vector<vector<int>> moves {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    for (char c : instructions) {
        if (c == 'G') {
            x += moves[dir][0];
            y += moves[dir][1];
        } else if (c == 'L') {
            dir = (dir + 3) % 4;
        } else {
            dir = (dir + 1) % 4;
        }
    }

    return (x == 0 && y == 0) || dir != 0;
}
  1. Initialize (x, y) as the robot's initial position and direction as dir (0: north, 1: east, 2: south, 3: west).
  2. Declare an array moves, which represents the movement in the x and y directions when going straight.
  3. Iterate through instructions:
    • If the instruction is 'G', update x and y based on the current direction.
    • If the instruction is 'L', update dir as (dir + 3) % 4 to turn left (counter-clockwise).
    • If the instruction is 'R', update dir as (dir + 1) % 4 to turn right (clockwise).
  4. If the robot reaches the starting position (0,0) or its direction is not north (dir != 0), then it stays in a circle. Otherwise, it doesn't.