Jewels and Stones
1 min read

Jewels and Stones

Given a string representing your stones and another string representing a list of jewels, return the number of stones that you have that are also jewels.

Ex: Given the following jewels and stones...

jewels = "abc", stones = "ac", return 2
jewels = "Af", stones = "AaaddfFf", return 3
jewels = "AYOPD", stones = "ayopd", return 0
  fun numJewelsInStones(jewels: String, stones: String): Int {
        var jewelSet = mutableSetOf<Char>()
        var jewelTotal = 0
        
        jewels.forEach{ item -> 
             jewelSet.add(item)
        }
        
         stones.forEach{ item ->
           if(jewelSet.contains(item)) {
                jewelTotal++
            }
        }
        return jewelTotal
    }

Enjoying these posts? Subscribe for more