Taro Logo

Remove Vowels from a String

Easy
Asked by:
Profile picture
2 views
Topics:
Strings

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 lowercase English letters only.

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Is the input string case-sensitive? Should I remove both uppercase and lowercase vowels?
  2. What characters are considered vowels? Are we only concerned with a, e, i, o, u, or should y also be considered?
  3. Can the input string be empty or null?
  4. Should I return a new string with the vowels removed, or modify the original string in place if that is possible?
  5. Besides standard ASCII characters, could the input string contain Unicode characters?

Brute Force Solution

Approach

The straightforward approach is to go through each letter of the given string one by one. For each letter, we'll check if it's a vowel and remove it if it is, constructing a new string without vowels.

Here's how the algorithm would work step-by-step:

  1. Take the first letter of the string.
  2. Check if the letter is a vowel (a, e, i, o, u).
  3. If it is a vowel, skip it and move on to the next letter.
  4. If it is not a vowel, keep it and add it to a new, separate string.
  5. Repeat steps 1-4 for every letter in the original string.
  6. The new string will contain all the non-vowel letters from the original string.

Code Implementation

def remove_vowels_brute_force(input_string):
    new_string_without_vowels = ""
    for char_index in range(len(input_string)):
        current_character = input_string[char_index]

        # Check if the current character is a vowel.
        if (current_character == 'a' or current_character == 'e' or
            current_character == 'i' or current_character == 'o' or
            current_character == 'u'):

            # If it is a vowel, skip adding it to the result.
            continue

        # Append the current character to the resulting string.
        new_string_without_vowels += current_character

    return new_string_without_vowels

Big(O) Analysis

Time Complexity
O(n)The algorithm iterates through each of the n characters in the input string. For each character, a constant-time check is performed to determine if it's a vowel. Appending a character to the new string also takes constant time. Therefore, the time complexity is directly proportional to the length of the string, resulting in O(n).
Space Complexity
O(N)The algorithm constructs a new string to store the non-vowel characters. In the worst-case scenario, where the input string contains no vowels, the new string will have the same length as the original string. Therefore, the space required for the new string grows linearly with the input size N, where N is the length of the input string. No other significant auxiliary space is used.

Optimal Solution

Approach

To efficiently remove vowels, we can inspect each character in the string one by one. If a character is a vowel, we skip it; otherwise, we keep it. By the end, we will have constructed a new string without vowels.

Here's how the algorithm would work step-by-step:

  1. Start with an empty new string where we will store our result.
  2. Look at each character in the original string, going from beginning to end.
  3. For each character, check if it's a vowel (a, e, i, o, u).
  4. If the character is a vowel, ignore it and move to the next character.
  5. If the character is not a vowel, add it to our new string.
  6. Once we have processed every character in the original string, our new string will contain only the non-vowel characters. This new string is the result.

Code Implementation

def remove_vowels_from_string(input_string):
    new_string_without_vowels = ""

    for char in input_string:
        # Check if the current char is a vowel.
        if char.lower() not in 'aeiou':

            # Append the non-vowel character to the new string.
            new_string_without_vowels += char

    # Return the new string containing only non-vowel characters.
    return new_string_without_vowels

Big(O) Analysis

Time Complexity
O(n)We iterate through the input string of size n character by character. For each character, we perform a constant-time vowel check. Appending to the result string also takes constant time per character. Therefore, the dominant operation is the single pass through the string, resulting in O(n) time complexity.
Space Complexity
O(N)The algorithm creates a new string to store the result without vowels. In the worst-case scenario, where the original string contains no vowels, the new string will have the same length as the original string, N. Therefore, the auxiliary space required to store the new string grows linearly with the input size N, where N is the length of the original string. The space usage is dominated by the size of the new string.

Edge Cases

CaseHow to Handle
Null input stringReturn null or throw an IllegalArgumentException as appropriate for the language and requirements.
Empty input stringReturn an empty string as the result of removing vowels from an empty string is still empty.
String containing only vowelsReturn an empty string as all characters are vowels and will be removed.
String containing no vowelsReturn the original string unchanged, as there are no vowels to remove.
String with mixed-case vowelsHandle both upper and lower case vowels (a, e, i, o, u, A, E, I, O, U).
Very long input stringEnsure the solution is efficient (e.g., O(n) time complexity) to handle large inputs without performance issues.
String with unicode characters including unicode vowelsThe solution should either handle the unicode vowels or clearly document that it only works with ASCII characters.
String containing only non-alphanumeric charactersThe solution should work correctly, treating these characters as non-vowels and leaving them untouched.