mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Add solution using Ruby .delete() method
This commit is contained in:
parent
27abece8ad
commit
89a7af33e1
1 changed files with 24 additions and 10 deletions
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue