mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-15 03:43:22 +01:00
Merge pull request #110 from jsca-kwok/jk-jewels-and-stones
This commit is contained in:
commit
04b7de56b2
2 changed files with 87 additions and 0 deletions
|
@ -12,6 +12,7 @@
|
|||
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
|
||||
* [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb)
|
||||
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
||||
* [Jewels & 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)
|
||||
* [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)
|
||||
|
|
86
data_structures/arrays/jewels_and_stones.rb
Normal file
86
data_structures/arrays/jewels_and_stones.rb
Normal file
|
@ -0,0 +1,86 @@
|
|||
# Challenge name: Jewels and Stones
|
||||
#
|
||||
# You're given strings jewels representing the types of stones that are jewels,
|
||||
# and stones representing the stones you have. Each character in stones is a type
|
||||
# of stone you have. You want to know how many of the stones you have are also
|
||||
# jewels.
|
||||
#
|
||||
# Letters are case sensitive, so "a" is considered a different type of stone from "A".
|
||||
#
|
||||
# Example 1:
|
||||
#
|
||||
# Input: jewels = "aA", stones = "aAAbbbb"
|
||||
# Output: 3
|
||||
#
|
||||
# Example 2:
|
||||
#
|
||||
# Input: jewels = "z", stones = "ZZ"
|
||||
# Output: 0
|
||||
#
|
||||
#
|
||||
# Constraints:
|
||||
#
|
||||
# 1 <= jewels.length, stones.length <= 50
|
||||
# jewels and stones consist of only English letters.
|
||||
# All the characters of jewels are unique.
|
||||
|
||||
#
|
||||
# Approach 1: Brute Force
|
||||
#
|
||||
# Time Complexity: O(n^2)
|
||||
#
|
||||
|
||||
def find_jewels(jewels, stones)
|
||||
jewels_array = jewels.split('')
|
||||
stones_array = stones.split('')
|
||||
result = 0
|
||||
jewels_array.each do |jewel|
|
||||
stones_array.each do |stone|
|
||||
if jewel == stone
|
||||
result += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
puts find_jewels("aA", "aAAbbbb")
|
||||
# => 3
|
||||
puts find_jewels("z", "ZZ")
|
||||
# => 0
|
||||
|
||||
#
|
||||
# Approach 2: Hash
|
||||
#
|
||||
# Time Complexity: O(n)
|
||||
#
|
||||
|
||||
def find_jewels(jewels, stones)
|
||||
jewels_array = jewels.split('')
|
||||
stones_array = stones.split('')
|
||||
result_hash = {}
|
||||
result = 0
|
||||
|
||||
stones_array.each do |stone|
|
||||
if result_hash[stone]
|
||||
result_hash[stone] += 1
|
||||
else
|
||||
result_hash[stone] = 1
|
||||
end
|
||||
end
|
||||
|
||||
jewels_array.each do |jewel|
|
||||
if result_hash[jewel]
|
||||
result += result_hash[jewel]
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
puts find_jewels("aA", "aAAbbbb")
|
||||
# => 3
|
||||
puts find_jewels("z", "ZZ")
|
||||
# => 0
|
Loading…
Reference in a new issue