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
'코딩 테스트 풀이 🛠' 카테고리의 다른 글
[Leet Code - eazy] 2413. Smallest Even Multiple (0) | 2023.01.29 |
---|---|
[Leet Code - eazy] 1672. Richest Customer Wealth (0) | 2023.01.28 |
[Leet Code - eazy] 1512. Number of Good Pairs (1) | 2023.01.28 |
[Leet Code - eazy] 1470. Shuffle the Array (0) | 2023.01.28 |
[프로그래머스] 같은 숫자는 싫어 (0) | 2023.01.22 |