코딩 테스트 풀이 🛠

[Leet Code - eazy] 771. Jewels and Stones

엄성준 2023. 1. 28. 00:40
728x90

You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

Letters are case sensitive, so "a" is considered a different type of stone from "A".

 

Example 1:

Input: jewels = "aA", stones = "aAAbbbb"
Output: 3

Example 2:

Input: jewels = "z", stones = "ZZ"
Output: 0

 

Constraints:

  • 1 <= jewels.length, stones.length <= 50
  • jewels and stones consist of only English letters.
  • All the characters of jewels are unique.

 

나의 풀이

 

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * @param {string} jewels
 * @param {string} stones
 * @return {number}
 */
var numJewelsInStones = function(jewels, stones) {
    let count = 0
    for(let i=0; i<stones.length; i++){
       if(jewels.includes(stones[i])) count++
    }
    return count
};
cs

 

문제를 풀며 느낀 점

 

- 문자열도 어떻게 보면 배열과 같기 때문에 for문을 통해서 문자열의 문자하나하나를 순회하면서 만약 jewels문자열에서 stones[i]문자를 포함한다면 count++을 통해서 count를 1씩 증가시켰고 반복문이 끝나면 count를 return 하였습니다.

728x90