From b4806f090372d4e2fadb66d3a5bb26eae031b02f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 09:48:52 -0700 Subject: [PATCH 1/8] Add remove vowels challenge --- DIRECTORY.md | 1 + data_structures/arrays/remove_vowels.rb | 29 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 data_structures/arrays/remove_vowels.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 8fea99b..4c10a66 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,6 +14,7 @@ * [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/jewels_and_stones.rb) * [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb) + * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_vowels.rb) * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb new file mode 100644 index 0000000..d1dbabf --- /dev/null +++ b/data_structures/arrays/remove_vowels.rb @@ -0,0 +1,29 @@ +# 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. +# Example 1: +# Input: s = "leetcodeisacommunityforcoders" +# Output: "ltcdscmmntyfrcdrs" +# +# Example 2: +# Input: s = "aeiou" +# Output: "" +# +# @param {String} s +# @return {String} + +# +# Approach 1: +# +# Time Complexity: +# +def remove_vowels(s) +end + +s = 'leetcodeisacommunityforcoders' +print(remove_vowels(s)) +# => "ltcdscmmntyfrcdrs" +s = 'aeiou' +print(remove_vowels(s)) +# => "" \ No newline at end of file From 8bafd5c74a329a86fd7f29954fb56fcb335d1a3c Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:03:45 -0700 Subject: [PATCH 2/8] Add brute force solution --- data_structures/arrays/remove_vowels.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d1dbabf..c5c7af8 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -14,11 +14,22 @@ # @return {String} # -# Approach 1: +# Approach 1: Brute Force # -# Time Complexity: +# Time Complexity: O(n) # def remove_vowels(s) + result_array = [] + s.downcase + 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('') end s = 'leetcodeisacommunityforcoders' From 27abece8ad34ba84d0d353e65e8d137e229da498 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:19:29 -0700 Subject: [PATCH 3/8] Add solution using regex --- data_structures/arrays/remove_vowels.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index c5c7af8..d9b800f 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -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" From 89a7af33e1a506f2f04e51808920a9b2f1ea27cb Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:34:28 -0700 Subject: [PATCH 4/8] Add solution using Ruby .delete() method --- data_structures/arrays/remove_vowels.rb | 34 +++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d9b800f..d8ddc31 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -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" From 5ed65199d74cb46fd2c187d4534f70ea313b6216 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 29 Mar 2021 09:59:32 -0700 Subject: [PATCH 5/8] Update data_structures/arrays/remove_vowels.rb Co-authored-by: vzvu3k6k --- data_structures/arrays/remove_vowels.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index d8ddc31..7a72f87 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -20,7 +20,7 @@ # def remove_vowels(s) result_array = [] - s.downcase + s.downcase! start_array = s.split('') start_array.each do |letter| @@ -71,4 +71,4 @@ print(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' print(remove_vowels(s)) -# => "" \ No newline at end of file +# => "" From 5bf141f64f65e0286d4a0ac3b2760cc13dceccf1 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 29 Mar 2021 10:00:33 -0700 Subject: [PATCH 6/8] Update data_structures/arrays/remove_vowels.rb Co-authored-by: vzvu3k6k --- data_structures/arrays/remove_vowels.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index 7a72f87..bd6da25 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -33,7 +33,7 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' print(remove_vowels(s)) From 31d9e8e0a22aefc791a912f14e145a2abd7257c0 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 29 Mar 2021 10:05:15 -0700 Subject: [PATCH 7/8] Change print to puts --- data_structures/arrays/remove_vowels.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/remove_vowels.rb index bd6da25..5fa39fc 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/remove_vowels.rb @@ -36,7 +36,7 @@ s = 'leetcodeisacommunityforcoders' puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" # @@ -51,10 +51,10 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" # @@ -67,8 +67,8 @@ def remove_vowels(s) end s = 'leetcodeisacommunityforcoders' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "ltcdscmmntyfrcdrs" s = 'aeiou' -print(remove_vowels(s)) +puts(remove_vowels(s)) # => "" From 3270667f8edc81b2d08d2058a4d35febb377f6e6 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 14:52:06 -0700 Subject: [PATCH 8/8] Move algorithsm to appropriate folder --- data_structures/arrays/{ => strings}/remove_vowels.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename data_structures/arrays/{ => strings}/remove_vowels.rb (99%) diff --git a/data_structures/arrays/remove_vowels.rb b/data_structures/arrays/strings/remove_vowels.rb similarity index 99% rename from data_structures/arrays/remove_vowels.rb rename to data_structures/arrays/strings/remove_vowels.rb index 5fa39fc..30d3042 100644 --- a/data_structures/arrays/remove_vowels.rb +++ b/data_structures/arrays/strings/remove_vowels.rb @@ -2,6 +2,7 @@ # # Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' # from it, and return the new string. +# # Example 1: # Input: s = "leetcodeisacommunityforcoders" # Output: "ltcdscmmntyfrcdrs" @@ -18,6 +19,7 @@ # # Time Complexity: O(n) # + def remove_vowels(s) result_array = [] s.downcase! @@ -57,7 +59,7 @@ s = 'aeiou' puts(remove_vowels(s)) # => "" -# +# # Approach 3: Using Ruby .delete() method # # Time Complexity: O(n)