spring-comes-to-us / algorithm-comes-to-us

알고리즘 스터디!
0 stars 0 forks source link

[LeetCode] 771. Jewels and Stones #33

Open shoeone96 opened 9 months ago

shoeone96 commented 9 months ago

문제 링크

스크린샷 2023-12-23 오후 2 15 36
yenzip commented 9 months ago

771. Jewels and Stones

코드 풀이

```java class Solution { public int numJewelsInStones(String jewels, String stones) { Set st = new HashSet<>(); int answer = 0; for(char j : jewels.toCharArray()) { st.add(j); } for(char s : stones.toCharArray()) { if(st.contains(s)) { answer++; } } return answer; } } ```

코멘트

shoeone96 commented 9 months ago

771. Jewels and Stones

코드 풀이

```java class Solution { public int numJewelsInStones(String jewels, String stones) { Map map = new HashMap<>(); int answer = 0; for(int i = 0; i < stones.length(); i++){ map.put(stones.charAt(i), map.getOrDefault(stones.charAt(i), 0) + 1); } for(int i = 0; i < jewels.length(); i++){ if(map.containsKey(jewels.charAt(i))){ answer += map.get(jewels.charAt(i)); } } return answer; } } ```

코멘트

Sehee-Lee-01 commented 9 months ago

image

Comment

Code

```java import java.util.HashMap; import java.util.Map; class Solution { public int numJewelsInStones(String jewels, String stones) { Map map = new HashMap<>(); for (char c : stones.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } int sumJewels = 0; for (char c : jewels.toCharArray()) { sumJewels += map.getOrDefault(c, 0); } return sumJewels; } } ```
ASak1104 commented 9 months ago

771. Jewels and Stones

Kotlin 풀이

```kt class Solution { val isJewels: IntArray = IntArray(58) fun numJewelsInStones(jewels: String, stones: String): Int { var count = 0 for (jewel in jewels) { isJewels[jewel - 'A'] = 1 } for (stone in stones) { count += isJewels[stone - 'A'] } return count } } ```

코멘트