일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- Binary Tree
- Stack
- Data Structure
- sorting
- Method
- database
- bit manipulation
- dynamic programming
- simulation
- Number Theory
- greedy
- implement
- 코딩테스트
- string
- 구현
- Counting
- java
- Binary Search
- 자바
- two pointers
- Tree
- geometry
- Matrix
- Math
- Class
- 파이썬
- array
- 코테
- hash table
- SQL
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.1] 카드 뭉치 본문
1. Input
1) String[] cards1
2) String[] cards2
3) String[] goal
2. Output
1) cards1과 cards2에 적힌 단어들로 goal를 만들 있다면 "Yes"를, 만들 수 없다면 "No"를 반환
// 이때, 카드에 적힌 단어 순서대로 goal을 만들 수 있어야 한다.
3. Constraint
1) 1 ≤ cards1의 길이, cards2의 길이 ≤ 10
2) 1 ≤ cards1[i]의 길이, cards2[i]의 길이 ≤ 10
3) cards1과 cards2에는 서로 다른 단어만 존재한다.
4) 2 ≤ goal의 길이 ≤ cards1의 길이 + cards2의 길이
5) 1 ≤ goal[i]의 길이 ≤ 10
6) goal의 원소는 cards1과 cards2의 원소들로만 이루어져 있다.
7) cards1, cards2, goal의 문자열들은 모두 알파벳 소문자로만 이루어져 있다.
4. Example
Input: cards1=["i", "drink", "water"], cards2=["want", "to"], goal=["i", "want", "to", "drink", "water"] -> Output: "Yes"
Input: cards1=["i", "water", "drink"], cards2=["want", "to"], goal=["i", "want", "to", "drink", "water"] -> Output: "Yes"
- 첫 번째 예시의 경우 cards1 -> 2 -> 2 -> 1 -> 1의 순서대로 단어를 꺼내면 goal을 만들 수 있다.
- 두 번째 예시의 경우 cards1 -> 2 -> 2로 "i want to"까지는 만들 수 있으나 "water"가 "drink"보다 먼저 사용되어야 하기 때문에 해당 문장을 완성시킬 수 없다.
5. Code
1) 첫 코드(2023/02/20)
String answer = "Yes";
int p1=0, p2=0;
for(int i=0 ; i<goal.length ; i++){
if(p1<cards1.length && goal[i].equals(cards1[p1])){
p1++;
} else if(p2<cards2.length && goal[i].equals(cards2[p2])){
p2++;
} else{
answer = "No"; break;
}
}
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Medium] 442. Find All Duplicates in an Array (0) | 2023.02.21 |
---|---|
[LeetCode/Medium] 438. Find All Anagrams in a String (0) | 2023.02.21 |
[LeetCode/Easy] 409. Longest Palindrome (0) | 2023.02.19 |
[LeetCode/Medium] 398. Random Pick Index (0) | 2023.02.16 |
[LeetCode/Medium] 384. Shuffle an Array (0) | 2023.02.15 |