> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/algorithm-problems/jewels-and-stones.md).

# 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하다.(분명하다.구별된다.뚜렷하다.)

{% hint style="info" %}

* S and J will consist of letters and have length at most 50.
* The characters in J are distinct.
  {% endhint %}

{% hint style="info" %}
Example 1 :\
Input: J = "aA", S = "aAAbbbb"\
Output : 3

Example 2 :\
Input : J = "z", S = "ZZ"\
Output : 0
{% endhint %}

Solution(내 생각)

자바에서 문자열을 비교하는 방법\
1\. equals를 이용 : 다른 프로그래밍 언어와는 다르게 자바에서 ==를 사용하면 객체(Object)가 동일한지를 체크하기 때문에 object가 갖는 문자열이 동일하다는 것은 보장하지 않기 때문이다.\
2\. compare 메소드 이용

**Solution(정답)**\
**사용할 자료구조** : **HashSet**\
**알고리즘**\
1\. **대소문자를 구분**하는 보석 문자를 갖고 있어야 한다. - aA : 2개\
2\. 스톤에 aA가 **개별적으로 몇 개 있는지** 확인한다.\
이 문제의 핵심은 java의 HashSet을 이용하는 것이다.**(순서 상관없고, 중복을 허용하지 않는다.)**

**알고리즘을 java언어로 구현**

```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 타입의 변수를 선언, 데이터 저장할 땐

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

2\. String의 **철자 하나하나**를 구분해서 비교해야 할 땐 **character** 타입의 set이 필요하다.

```java
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()                 | 저장된 객체의 개수를 반환한다.                    |

<br>
