771 LeetCode gems and stones

Description Title:
Here Insert Picture Description
idea:
1, with a number of different types of hash table records stones
2, in the gem and find if there is recorded the number of

code show as below:

class Solution {
public:
    int numJewelsInStones(string J, string S) {
        map<char,int>cnt;
        int result=0;
        for(int i=0;i<S.size();i++){
            cnt[S[i]]++;
        }
        for(int i=0;i<J.size();i++){
            if(cnt[J[i]]>0)
            result+=cnt[J[i]];
        }
        return result;
    }
};
Published 114 original articles · won praise 0 · Views 820

Guess you like

Origin blog.csdn.net/peachzy/article/details/104360550