Problem of The Day: Remove Vowels from a String
Problem Statement
Intuition
My initial thought is to iterate through the given string and remove any vowels encountered.
Approach
To implement this, I’ll loop through each character in the string. If the character is not a vowel (i.e., not ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), I’ll add it to a result list. Finally, I’ll join the characters in the result list to form the final string without vowels.
Complexity
-
Time complexity: O(n), where n is the length of the input string. This is because we iterate through the string once.
-
Space complexity: O(n), where n is the length of the input string. This is due to the result list that holds the characters without vowels.
Code
class Solution:
def removeVowels(self, s: str) -> str:
res = []
for c in s:
if c not in {'a', 'e', 'i', 'o', 'u'}:
res.append(c)
return ''.join(res)