Leetcode-771 gems and stones-easy

771 Gems and Stones
Title Description

The given string J represents the type of gem in the stone, and the string S represents the stone you own.
Each character in S represents a type of stone you own, and you want to know how many of the stones you own are gems.

The letters in J are not repeated, and all the characters in J and S are letters. Letters are case-sensitive, so "a" and "A" are different types of stones.

Link: https://leetcode-cn.com/problems/jewels-and-stones

Example 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3

Example 2:

输入: J = "z", S = "ZZ"
输出: 0
注意:
S 和 J 最多含有50个字母。
 J 中的字符不重复。

Problem-solving ideas:
Use Map to store which characters have appeared in the J string, traverse the S string to see if it appears in the Map, Sum is used for counting, if it appears in the Map, Sum is increased by 1. Finally return to Sum.

func numJewelsInStones(J string, S string) int {
    
    
    Map := make(map[rune]bool)
    Sum := 0
    for _,val := range J {
    
    
        Map[val] = true
    }
    for _,val := range S {
    
    
        if(Map[val]){
    
    
            Sum ++
        }
    }
    return Sum
}

Insert picture description here

Guess you like

Origin blog.csdn.net/Yang_1998/article/details/108541489