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('') result_array.join('')
end end
# s = 'leetcodeisacommunityforcoders' s = 'leetcodeisacommunityforcoders'
# print(remove_vowels(s)) print(remove_vowels(s))
# # => "ltcdscmmntyfrcdrs" # => "ltcdscmmntyfrcdrs"
# s = 'aeiou' s = 'aeiou'
# print(remove_vowels(s)) print(remove_vowels(s))
# # => "" # => ""
# #
# Approach 2: Regex # Approach 2: Regex
@ -45,13 +45,27 @@ end
# Time Complexity: O(n) # Time Complexity: O(n)
# #
def remove_vowels(s) def remove_vowels(s)
vowels = /[aeiou]+/ vowels = /[aeiou]/i
s.scan(vowels).each do |letter| s.gsub!(vowels, '')
s.sub!(letter, '')
end
s s
end 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' s = 'leetcodeisacommunityforcoders'
print(remove_vowels(s)) print(remove_vowels(s))
# => "ltcdscmmntyfrcdrs" # => "ltcdscmmntyfrcdrs"