Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

771. Jewels and Stones #141

Open
Tcdian opened this issue May 2, 2020 · 1 comment
Open

771. Jewels and Stones #141

Tcdian opened this issue May 2, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented May 2, 2020

771. Jewels and Stones

 给定字符串J 代表石头中宝石的类型,和字符串 S 代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

J 中的字母不重复,J 和 S 中的所有字符都是字母。字母区分大小写,因此"a""A"是不同类型的石头。

Example 1

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2

Input: J = "z", S = "ZZ"
Output: 0

Note

  • SJ 最多含有50个字母。
  • J 中的字符不重复。
@Tcdian
Copy link
Owner Author

Tcdian commented May 2, 2020

Solution

  • JavaScript Solution
/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    const jewels = new Set();
    let result = 0;
    for (let i = 0; i < J.length; i++) {
        jewels.add(J[i]);
    }
    for (let i = 0; i < S.length; i++) {
        if (jewels.has(S[i])) {
            result++;
        }
    }
    return result;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant