Add hash solution

This commit is contained in:
Jessica Kwok 2021-05-14 16:36:04 -07:00
parent 0386dfd0d1
commit 4bd9206f01
2 changed files with 31 additions and 2 deletions

View file

@ -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)

View file

@ -16,4 +16,33 @@
#
# Example 2:
# Input: A = "apple apple", B = "banana"
# Output: ["banana"]
# 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")