2021-03-26 17:48:52 +01:00
|
|
|
# Challenge name: Remove vowels from a string
|
|
|
|
#
|
|
|
|
# Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u'
|
|
|
|
# from it, and return the new string.
|
2021-03-29 23:52:06 +02:00
|
|
|
#
|
2021-03-26 17:48:52 +01:00
|
|
|
# Example 1:
|
|
|
|
# Input: s = "leetcodeisacommunityforcoders"
|
|
|
|
# Output: "ltcdscmmntyfrcdrs"
|
|
|
|
#
|
|
|
|
# Example 2:
|
|
|
|
# Input: s = "aeiou"
|
|
|
|
# Output: ""
|
|
|
|
#
|
|
|
|
# @param {String} s
|
|
|
|
# @return {String}
|
|
|
|
|
|
|
|
#
|
2021-03-26 18:03:45 +01:00
|
|
|
# Approach 1: Brute Force
|
2021-03-26 17:48:52 +01:00
|
|
|
#
|
2021-03-26 18:03:45 +01:00
|
|
|
# Time Complexity: O(n)
|
2021-03-26 17:48:52 +01:00
|
|
|
#
|
2021-03-29 23:52:06 +02:00
|
|
|
|
2021-03-26 17:48:52 +01:00
|
|
|
def remove_vowels(s)
|
2021-03-26 18:03:45 +01:00
|
|
|
result_array = []
|
2021-03-29 18:59:32 +02:00
|
|
|
s.downcase!
|
2021-03-26 18:03:45 +01:00
|
|
|
start_array = s.split('')
|
|
|
|
|
|
|
|
start_array.each do |letter|
|
|
|
|
if letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u'
|
|
|
|
result_array.push(letter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result_array.join('')
|
2021-03-26 17:48:52 +01:00
|
|
|
end
|
|
|
|
|
2021-03-26 18:34:28 +01:00
|
|
|
s = 'leetcodeisacommunityforcoders'
|
2021-03-29 19:00:33 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-26 18:34:28 +01:00
|
|
|
# => "ltcdscmmntyfrcdrs"
|
|
|
|
s = 'aeiou'
|
2021-03-29 19:05:15 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-26 18:34:28 +01:00
|
|
|
# => ""
|
2021-03-26 18:19:29 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Approach 2: Regex
|
|
|
|
#
|
|
|
|
# Time Complexity: O(n)
|
|
|
|
#
|
|
|
|
def remove_vowels(s)
|
2021-03-26 18:34:28 +01:00
|
|
|
vowels = /[aeiou]/i
|
|
|
|
s.gsub!(vowels, '')
|
2021-03-26 18:19:29 +01:00
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2021-03-26 18:34:28 +01:00
|
|
|
s = 'leetcodeisacommunityforcoders'
|
2021-03-29 19:05:15 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-26 18:34:28 +01:00
|
|
|
# => "ltcdscmmntyfrcdrs"
|
|
|
|
s = 'aeiou'
|
2021-03-29 19:05:15 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-26 18:34:28 +01:00
|
|
|
# => ""
|
|
|
|
|
2021-03-29 23:52:06 +02:00
|
|
|
#
|
2021-03-26 18:34:28 +01:00
|
|
|
# Approach 3: Using Ruby .delete() method
|
|
|
|
#
|
|
|
|
# Time Complexity: O(n)
|
|
|
|
#
|
|
|
|
def remove_vowels(s)
|
|
|
|
s.downcase.delete('aeiou')
|
|
|
|
end
|
|
|
|
|
2021-03-26 17:48:52 +01:00
|
|
|
s = 'leetcodeisacommunityforcoders'
|
2021-03-29 19:05:15 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-26 17:48:52 +01:00
|
|
|
# => "ltcdscmmntyfrcdrs"
|
|
|
|
s = 'aeiou'
|
2021-03-29 19:05:15 +02:00
|
|
|
puts(remove_vowels(s))
|
2021-03-29 18:59:32 +02:00
|
|
|
# => ""
|