Leetcode Note: Go - Jewels and Stones

Jewels and Stones - LeetCode
https://leetcode.com/problems/jewels-and-stones/

  • Go 言語で取り組んだメモ

所感

  • 宝石を表す文字列が渡されるので、宝石の数をカウントして int で return する

回答

[Go] O(N) time | 0ms (100%), 2.1mb - LeetCode Discuss
https://leetcode.com/problems/jewels-and-stones/discuss/1301063/Go-O(N)-time-or-0ms-(100)-2.1mb

func numJewelsInStones(jewels string, stones string) int {
    jewelsMap := map[rune]bool{}
    for _, jewel := range jewels {
        jewelsMap[jewel] = true
    }
    
    result := 0
    for _, stone := range stones {
        if jewelsMap[stone] {
            result++
        }
    }
    return result
}