> For the complete documentation index, see [llms.txt](https://devlee.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devlee.gitbook.io/docs/study/99/99-11-til.md).

# \[99클럽 코테 스터디 11일차 TIL]  프로그래머스 - 카드 뭉치

## \[level 1] 카드 뭉치 - 159994

[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/159994)

#### 문제 설명

코니는 영어 단어가 적힌 카드 뭉치 두 개를 선물로 받았습니다. 코니는 다음과 같은 규칙으로 카드에 적힌 단어들을 사용해 원하는 순서의 단어 배열을 만들 수 있는지 알고 싶습니다.

* 원하는 카드 뭉치에서 카드를 순서대로 한 장씩 사용합니다.
* 한 번 사용한 카드는 다시 사용할 수 없습니다.
* 카드를 사용하지 않고 다음 카드로 넘어갈 수 없습니다.
* 기존에 주어진 카드 뭉치의 단어 순서는 바꿀 수 없습니다.

예를 들어 첫 번째 카드 뭉치에 순서대로 \["i", "drink", "water"], 두 번째 카드 뭉치에 순서대로 \["want", "to"]가 적혀있을 때 \["i", "want", "to", "drink", "water"] 순서의 단어 배열을 만들려고 한다면 첫 번째 카드 뭉치에서 "i"를 사용한 후 두 번째 카드 뭉치에서 "want"와 "to"를 사용하고 첫 번째 카드뭉치에 "drink"와 "water"를 차례대로 사용하면 원하는 순서의 단어 배열을 만들 수 있습니다.

문자열로 이루어진 배열 `cards1`, `cards2`와 원하는 단어 배열 `goal`이 매개변수로 주어질 때, `cards1`과 `cards2`에 적힌 단어들로 `goal`를 만들 있다면 "Yes"를, 만들 수 없다면 "No"를 return하는 solution 함수를 완성해주세요.

***

**제한사항**

* 1 ≤ `cards1`의 길이, `cards2`의 길이 ≤ 10
  * 1 ≤ `cards1[i]`의 길이, `cards2[i]`의 길이 ≤ 10
  * `cards1`과 `cards2`에는 서로 다른 단어만 존재합니다.
* 2 ≤ `goal`의 길이 ≤ `cards1`의 길이 + `cards2`의 길이
  * 1 ≤ `goal[i]`의 길이 ≤ 10
  * `goal`의 원소는 `cards1`과 `cards2`의 원소들로만 이루어져 있습니다.
* `cards1`, `cards2`, `goal`의 문자열들은 모두 알파벳 소문자로만 이루어져 있습니다.

***

**입출력 예**

| cards1                   | cards2          | goal                                   | result |
| ------------------------ | --------------- | -------------------------------------- | ------ |
| \["i", "drink", "water"] | \["want", "to"] | \["i", "want", "to", "drink", "water"] | "Yes"  |
| \["i", "water", "drink"] | \["want", "to"] | \["i", "want", "to", "drink", "water"] | "No"   |

***

**입출력 예 설명**

입출력 예 #1

본문과 같습니다.

입출력 예 #2

`cards1`에서 "i"를 사용하고 `cards2`에서 "want"와 "to"를 사용하여 "i want to"까지는 만들 수 있지만 "water"가 "drink"보다 먼저 사용되어야 하기 때문에 해당 문장을 완성시킬 수 없습니다. 따라서 "No"를 반환합니다.

***

## 풀이 코드

```java
class Solution {
    public String solution(String[] cards1, String[] cards2, String[] goal) {
        String answer = "Yes";
        int cardIdx1 = 0;
        int cardIdx2 = 0;
        
        for (int i = 0; i < goal.length; i++) {
            String g = goal[i];
            if (cardIdx1 < cards1.length && g.equals(cards1[cardIdx1])) cardIdx1++;
            else if (cardIdx2 < cards2.length && g.equals(cards2[cardIdx2])) cardIdx2++;
            else return "No";
        }
        
        return answer;
    }
}
```

* Time: 0.04 ms
* Memory: 77.8 MB

먼저 `goal` 배열을 돌면서 `goal` 배열의 단어가 각 카드 뭉치에 있는지 확인해야 한다. 이때 확인해야할 각 카드 뭉치의 단어는 사용하지 않은 단어여야 한다. 이를 알기 위해서는 사용했다는 표시가 필요하다고 생각했고 각 카드 뭉치의 시작 인덱스 값을 선언해서 `goal` 배열의 단어와 일치하면 하나씩 증가하는 식으로 해결이 가능할 것 같았다.

그렇게 테스트를 돌렸으나 `OutOfBoundsException` 이 터졌다. 이유는 입출력 예 2번의 입력 값을 제대로 처리하지 못했기 때문인데 에러가 발생된 흐름은 아래와 같다.

1. `"i"` 값과 동일한 단어가 `cards1` 카드 뭉치의 0번째 인덱스에 있다. - `cardIdx1` = 1
2. `"want"` 값과 동일한 단어가 `cards2` 카드 뭉치의 0번째 인덱스에 있다. - `cardIdx2` = 1
3. `"to"` 값과 동일한 단어가  `cards2` 카드 뭉치의 1번째 인덱스에 있다. - `cardIdx2` = 2
4. `"water"` 값과 동일한 단어가 `cards1` 카드 뭉치의 1번째 인덱스에 없다.
5. `"water"` 값과 동일한 단어가 `cards2` 카드 뭉치의 2... error

위에서처럼 `cards2` 배열을 넘어서는 2번째 인덱스 값을 참조하려다가 에러가 발생한다. 그래서 조건식에 각 카드 뭉치의 인덱스 값 `cardIdx1` 과 `cardIdx2` 는 각 배열의 길이를 넘을 수 없다는 조건을 추가해서 통과했다.

***

## 돌아보기

퇴근길에 풀이를 머리로 생각하면서 왔는데 스스로 참 맘에 드는 풀이다. 재밌었다.

***

\#99클럽 #코딩테스트준비 #개발자취업 #항해99 #TIL


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devlee.gitbook.io/docs/study/99/99-11-til.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
