Jewels and Stones

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

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

=> jewels์ธ ์Šคํ†ค ํ˜•ํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ŠคํŠธ๋ง J์™€ ์Šคํ†ค์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ŠคํŠธ๋ง S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. S์˜ ๊ฐ๊ฐ ์ฒ ์ž๋Š” ๋‹น์‹ ์ด ๊ฐ€์ง„ ์Šคํ†ค์ด๋‹ค. ๋‹น์‹ ์€ ๋‹น์‹ ์ด ๊ฐ€์ง„ ์Šคํ†ค ์ค‘ jewel์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค.

J์˜ ๋ฌธ์ž์—ด๋“ค์€ ๊ตฌ๋ณ„๋˜๊ณ , J์™€ S ์•ˆ์˜ ๋ชจ๋“  ์ฒ ์ž๋“ค์€ ๊ธ€์ž์ด๋‹ค. ๊ธ€์ž๋“ค์€ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋œ๋‹ค.

S์™€ J๋Š” ์ตœ๋Œ€ 50 ๊ธธ์ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. J ์•ˆ์˜ character๋“ค์€ distinctํ•˜๋‹ค.(๋ถ„๋ช…ํ•˜๋‹ค.๊ตฌ๋ณ„๋œ๋‹ค.๋šœ๋ ทํ•˜๋‹ค.)

  • S and J will consist of letters and have length at most 50.

  • The characters in J are distinct.

Example 1 : Input: J = "aA", S = "aAAbbbb" Output : 3

Example 2 : Input : J = "z", S = "ZZ" Output : 0

Solution(๋‚ด ์ƒ๊ฐ)

์ž๋ฐ”์—์„œ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ• 1. equals๋ฅผ ์ด์šฉ : ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ž๋ฐ”์—์„œ ==๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด(Object)๊ฐ€ ๋™์ผํ•œ์ง€๋ฅผ ์ฒดํฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— object๊ฐ€ ๊ฐ–๋Š” ๋ฌธ์ž์—ด์ด ๋™์ผํ•˜๋‹ค๋Š” ๊ฒƒ์€ ๋ณด์žฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 2. compare ๋ฉ”์†Œ๋“œ ์ด์šฉ

Solution(์ •๋‹ต) ์‚ฌ์šฉํ•  ์ž๋ฃŒ๊ตฌ์กฐ : HashSet ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1. ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ณด์„ ๋ฌธ์ž๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. - aA : 2๊ฐœ 2. ์Šคํ†ค์— aA๊ฐ€ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ java์˜ HashSet์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.(์ˆœ์„œ ์ƒ๊ด€์—†๊ณ , ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.)

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ java์–ธ์–ด๋กœ ๊ตฌํ˜„

class JewelStones {
    public static void main(String[] args) {
        String J = "a,A", S = "a,A,A,b,b,b,b";
        System.out.println("The number of jewels : " + solve(J,S));
        //int result = solve(s1,s2);๊ตณ์ด ๋ณ€์ˆ˜ ์•ˆ๋งŒ๋“ค์–ด๋„ ๋  ์ •๋„๋กœ ๊ฐ„๋‹จ.
    }
    public static int solve(String jew, String stone) {
        int cnt = 0;
        Set<Character> set = new HashSet<>();
        
        //1.HashSet์— jewel ๋ฌธ์ž์—ด์„ ๋‹ด๋Š”๋‹ค.
        for(char jewelChar : jew.toCharArray()) {
            set.add(jewelChar);//a,A๊ฐ€ HashSet์— ๋‹ด๊ธด๋‹ค.
            System.out.println("char in HashSet :"+jewelChar);
        }
        //2. stone์— jewel์ด ์–ผ๋งˆ๋‚˜ ๋“ค์–ด์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค!
        for(char stoneChar : stone.toCharArray()) {
            System.out.println("stoneChar :"+stoneChar);
            if(set.contains(stoneChar) {
                cnt++;
            }
        }
        return cnt;
    }
}

๋ฐฐ์šด ๋‚ด์šฉ ์ •๋ฆฌ

  1. String ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ, ๋ฐ์ดํ„ฐ ์ €์žฅํ•  ๋•

String name = "H,e,u,n,n,a";

2. String์˜ ์ฒ ์ž ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ๋น„๊ตํ•ด์•ผ ํ•  ๋• character ํƒ€์ž…์˜ set์ด ํ•„์š”ํ•˜๋‹ค.

Set<Character> set = new HashSet<>();

3. Java์˜ HashSet

HashSet : ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์› > Set์ธํ„ฐํŽ˜์ด์Šค > HashSet HashSet์€ Set ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์ปฌ๋ ‰์…˜์ด๋ฉฐ, ์ˆœ์„œ๋ฅผ ์ƒ๊ด€ํ•˜์ง€ ์•Š๊ณ  ์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ˆœ์„œX, ์ค‘๋ณตX

์ƒ์„ฑ์ž ๋˜๋Š” ๋ฉ”์†Œ๋“œ

์„ค๋ช…

HashSet()

HashSet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

boolean add(Object o)

์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.(์„ฑ๊ณตํ•˜๋ฉด true, ์‹คํŒจํ•˜๋ฉด false)

boolean contains(Object o)

์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.

boolean isEmpty()

HashSet์ด ๋น„์–ด์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.

int size()

์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Last updated