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