Add solution using Ruby .delete() method

This commit is contained in:
Jessica Kwok 2021-03-26 10:34:28 -07:00
parent 27abece8ad
commit 89a7af33e1

View file

@ -32,12 +32,12 @@ def remove_vowels(s)
result_array.join('')
end
# s = 'leetcodeisacommunityforcoders'
# print(remove_vowels(s))
# # => "ltcdscmmntyfrcdrs"
# s = 'aeiou'
# print(remove_vowels(s))
# # => ""
s = 'leetcodeisacommunityforcoders'
print(remove_vowels(s))
# => "ltcdscmmntyfrcdrs"
s = 'aeiou'
print(remove_vowels(s))
# => ""
#
# Approach 2: Regex
@ -45,13 +45,27 @@ end
# Time Complexity: O(n)
#
def remove_vowels(s)
vowels = /[aeiou]+/
s.scan(vowels).each do |letter|
s.sub!(letter, '')
end
vowels = /[aeiou]/i
s.gsub!(vowels, '')
s
end
s = 'leetcodeisacommunityforcoders'
print(remove_vowels(s))
# => "ltcdscmmntyfrcdrs"
s = 'aeiou'
print(remove_vowels(s))
# => ""
#
# Approach 3: Using Ruby .delete() method
#
# Time Complexity: O(n)
#
def remove_vowels(s)
s.downcase.delete('aeiou')
end
s = 'leetcodeisacommunityforcoders'
print(remove_vowels(s))
# => "ltcdscmmntyfrcdrs"