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