-
[알고리즘] Jewels And Stones알고리즘/배열 2021. 4. 20. 10:21
Problem
Key Point
- Set 컬렉션
- 요소의 저장 순서를 유지하지 않는다.
- 같은 요소의 중복 저장을 허용하지 않는다.
Set<Character> charSet = new HashSet<>();
Code
public class JewelsAndStones { public int solution(String jewels, String stones) { int count = 0; Set<Character> jset = new HashSet<>(); for (char jewel : jewels.toCharArray()) { jset.add(jewel); } for (char stone : stones.toCharArray()) { if (jset.contains(stone)) { count++; } } return count; } public static void main(String[] args) { JewelsAndStones jewelsAndStones = new JewelsAndStones(); System.out.println(jewelsAndStones.solution("aA", "aAAbbbb")); } }
'알고리즘 > 배열' 카테고리의 다른 글
[알고리즘] Merge Intervals (O) (0) 2021.04.20 [알고리즘] Meeting Rooms II (0) 2021.04.20 [알고리즘] License Key Formatting (0) 2021.04.19 [알고리즘] K Closest Points to Origin (0) 2021.04.19 [알고리즘] Plus One (0) 2021.04.19 - Set 컬렉션