From 0386dfd0d1b86dd7bf4bf7b99be88f7847f0aecd Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 13 May 2021 12:09:01 -0700 Subject: [PATCH 1/4] Add uncommon words challenge --- DIRECTORY.md | 1 + .../arrays/strings/uncommon_words.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 data_structures/arrays/strings/uncommon_words.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..115e65c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -24,6 +24,7 @@ * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) + * [Uncommon Words](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/uncommon_words.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) * [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb) * Binary Trees diff --git a/data_structures/arrays/strings/uncommon_words.rb b/data_structures/arrays/strings/uncommon_words.rb new file mode 100644 index 0000000..32fa391 --- /dev/null +++ b/data_structures/arrays/strings/uncommon_words.rb @@ -0,0 +1,19 @@ +# Challenge name: Uncommon words from two sentences +# +# We are given two sentences A and B. +# (A sentence is a string of space separated words. +# Each word consists only of lowercase letters.) +# +# A word is uncommon if it appears exactly once in one of the sentences, +# and does not appear in the other sentence. +# +# Return a list of all uncommon words. +# You may return the list in any order. +# +# Example 1: +# Input: A = "this apple is sweet", B = "this apple is sour" +# Output: ["sweet","sour"] +# +# Example 2: +# Input: A = "apple apple", B = "banana" +# Output: ["banana"] \ No newline at end of file From 4bd9206f018fd6693775aa1b5ac2ab002b008515 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 14 May 2021 16:36:04 -0700 Subject: [PATCH 2/4] Add hash solution --- DIRECTORY.md | 2 +- .../strings => hash_table}/uncommon_words.rb | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) rename data_structures/{arrays/strings => hash_table}/uncommon_words.rb (55%) diff --git a/DIRECTORY.md b/DIRECTORY.md index 115e65c..a3bafbe 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -24,7 +24,6 @@ * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) - * [Uncommon Words](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/uncommon_words.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) * [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb) * Binary Trees @@ -36,6 +35,7 @@ * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb) * [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb) + * [Uncommon Words](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/uncommon_words.rb) * Linked Lists * [Circular Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/circular_linked_list.rb) * [Doubly Linked List](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/linked_lists/doubly_linked_list.rb) diff --git a/data_structures/arrays/strings/uncommon_words.rb b/data_structures/hash_table/uncommon_words.rb similarity index 55% rename from data_structures/arrays/strings/uncommon_words.rb rename to data_structures/hash_table/uncommon_words.rb index 32fa391..e3e8d2f 100644 --- a/data_structures/arrays/strings/uncommon_words.rb +++ b/data_structures/hash_table/uncommon_words.rb @@ -16,4 +16,33 @@ # # Example 2: # Input: A = "apple apple", B = "banana" -# Output: ["banana"] \ No newline at end of file +# Output: ["banana"] + +# +# Approach 1: Hash +# +# Time Complexitiy: O(n) + +def find_uncommon_words(strA, strB) + array = strA.concat(" ", strB).split(" ") + hash = Hash.new(0) + result = [] + + array.each do |word| + if hash[word] + hash[word] += 1 + else + hash[word] = 1 + end + end + + hash.each do |k, v| + if v < 2 + result.push(k) + end + end + + result +end + +puts find_uncommon_words("this apple is sweet", "this apple is sour") \ No newline at end of file From a591b2fd5626c6fabfa8e5a56f6fb6b5d54c5cef Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 14 May 2021 16:53:05 -0700 Subject: [PATCH 3/4] Add examples --- data_structures/hash_table/uncommon_words.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/data_structures/hash_table/uncommon_words.rb b/data_structures/hash_table/uncommon_words.rb index e3e8d2f..9e17b61 100644 --- a/data_structures/hash_table/uncommon_words.rb +++ b/data_structures/hash_table/uncommon_words.rb @@ -45,4 +45,8 @@ def find_uncommon_words(strA, strB) result end -puts find_uncommon_words("this apple is sweet", "this apple is sour") \ No newline at end of file +puts find_uncommon_words("this apple is sweet", "this apple is sour") +# => ["sweet", "sour"] + +puts find_uncommon_words("apple apple", "banana") +# => ["banana"] \ No newline at end of file From 7f6b015a3bd847d2c1cb4f7e34daa6c766787706 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Mon, 17 May 2021 09:32:35 -0700 Subject: [PATCH 4/4] Update data_structures/hash_table/uncommon_words.rb Co-authored-by: Vitor Oliveira --- data_structures/hash_table/uncommon_words.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/data_structures/hash_table/uncommon_words.rb b/data_structures/hash_table/uncommon_words.rb index 9e17b61..ff0ace9 100644 --- a/data_structures/hash_table/uncommon_words.rb +++ b/data_structures/hash_table/uncommon_words.rb @@ -29,11 +29,7 @@ def find_uncommon_words(strA, strB) result = [] array.each do |word| - if hash[word] - hash[word] += 1 - else - hash[word] = 1 - end + hash[word] += 1 end hash.each do |k, v| @@ -49,4 +45,4 @@ puts find_uncommon_words("this apple is sweet", "this apple is sour") # => ["sweet", "sour"] puts find_uncommon_words("apple apple", "banana") -# => ["banana"] \ No newline at end of file +# => ["banana"]