본문 바로가기
programming/알고리즘 풀이

[leetcode] 771. Jewels and Stones

by 몽구스_ 2021. 6. 21.
728x90

 

 

 

Jewels and Stones - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 

a가 stones에 몇개 있는지, A가 stones에 몇개 있는지 찾는 문제다.

대소문자를 다르게 취급해주기때문에 별로 어렵지 않다.

처음에 문제를 이해하기 약간 어려울 수 있다.

 

 

class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        int result = 0;
		
		String[] J = jewels.split("");
		
		for(int i = 0; i < J.length; i++) {
			
			
			for(int j = 0; j < stones.length(); j++) {
				
				if(stones.charAt(j) == J[i].charAt(0)) {
					result++;
				}

			}


		}
		
		return result;
    }
}

 

jewels의 요소를 split으로 잘라서 하나씩 배열에 넣어주고

첫배열부터 차례대로 stones배열과 비교한 후, 같으면 result에 1씩 더해주었다.

 

 

댓글