λ¬Έμ
νμ΄ κ³Όμ
νΉμ λ¬Έμμ΄μ ν¬ν¨λ λ¬Έμμ κ°μλ₯Ό μ°Ύλ λ¬Έμ μ
λλ€.
λ¬Έμ νμμ κΈ°μ€μ΄ λλ λ¬Έμμ΄ J
λ₯Ό ν΄μ±ν΄μ μ μ₯ν λ€μ S
λ₯Ό νλμ© μννλ©° ν΄λΉ λ¬Έμκ° J
μ μ‘΄μ¬νλμ§ μ°Ύμ΅λλ€.
μ΄λ κ² νλ©΄ O(N)
μ νμμ μνν μ μμ΅λλ€.
μ½λ
/**
* @param {string} J
* @param {string} S
* @return {number}
*/
var numJewelsInStones = function (J, S) {
const hashTable = {};
[...J].forEach((c) => (hashTable[c] = 1));
const answer = [...S].filter((c) => hashTable[c]);
return answer.length;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 19 - Remove Nth Node From End of List (Medium) (0) | 2021.03.03 |
---|---|
LeetCode 79 - Word Search (Medium) (0) | 2021.03.03 |
LeetCode 5 - Longest Palindromic Substring (Medium) (0) | 2021.03.03 |
LeetCode 206 - Reverse Linked List (Easy) (0) | 2021.03.03 |
LeetCode 136 - Single Number (Easy) (0) | 2021.03.03 |
π¬ λκΈ