mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +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('')
|
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"
|
||||||
|
|
Loading…
Reference in a new issue