Add solution using regex

This commit is contained in:
Jessica Kwok 2021-03-26 10:19:29 -07:00
parent 8bafd5c74a
commit 27abece8ad

View file

@ -32,6 +32,26 @@ def remove_vowels(s)
result_array.join('')
end
# s = 'leetcodeisacommunityforcoders'
# print(remove_vowels(s))
# # => "ltcdscmmntyfrcdrs"
# s = 'aeiou'
# print(remove_vowels(s))
# # => ""
#
# Approach 2: Regex
#
# Time Complexity: O(n)
#
def remove_vowels(s)
vowels = /[aeiou]+/
s.scan(vowels).each do |letter|
s.sub!(letter, '')
end
s
end
s = 'leetcodeisacommunityforcoders'
print(remove_vowels(s))
# => "ltcdscmmntyfrcdrs"