771. Jewels and Stones - LeetCode

Question

771. Jewels and Stones java

Solution

题目大意:两个字符串J和S,其中J中每一个字符不一样,求S中包含有J中字符的个数,重复的也算code

思路:Set记录字符串J中的每一个字符,遍历S中的字符,若是出如今Set中,count加1ip

Java实现:leetcode

public int numJewelsInStones(String J, String S) {
    Set<Character> set = new HashSet<>();
    int count = 0;
    for (char c : J.toCharArray()) {
        set.add(c);
    }
    for (char c : S.toCharArray()) {
        if (set.contains(c)) {
            count++;
        }
    }
    return count;
}
相关文章
相关标签/搜索