mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-27 21:58:57 +01:00
Add hash solution
This commit is contained in:
parent
2032eef824
commit
60d4a4bcbc
1 changed files with 35 additions and 3 deletions
|
@ -9,11 +9,43 @@
|
||||||
# You may return the answer in any order.
|
# You may return the answer in any order.
|
||||||
#
|
#
|
||||||
# Example 1:
|
# Example 1:
|
||||||
#
|
|
||||||
# Input: ["bella","label","roller"]
|
# Input: ["bella","label","roller"]
|
||||||
# Output: ["e","l","l"]
|
# Output: ["e","l","l"]
|
||||||
#
|
#
|
||||||
# Example 2:
|
# Example 2:
|
||||||
#
|
|
||||||
# Input: ["cool","lock","cook"]
|
# Input: ["cool","lock","cook"]
|
||||||
# Output: ["c","o"]
|
# Output: ["c","o"]
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 1: Hash
|
||||||
|
#
|
||||||
|
# Time Complexity: O(n)
|
||||||
|
#
|
||||||
|
def common_characters(arr)
|
||||||
|
target_count = arr.count
|
||||||
|
|
||||||
|
hash = Hash.new(0)
|
||||||
|
(0...target_count).each do |i|
|
||||||
|
arr[i].split('').each do |letter|
|
||||||
|
hash[letter] += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
result = []
|
||||||
|
hash.each do |k, v|
|
||||||
|
while v >= target_count
|
||||||
|
if v >= target_count
|
||||||
|
result << k
|
||||||
|
v -= target_count
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
puts common_characters(["bella","label","roller"])
|
||||||
|
# => ["e","l","l"]
|
||||||
|
|
||||||
|
puts common_characters(["cool","lock","cook"])
|
||||||
|
# => ["c","o"]
|
Loading…
Reference in a new issue