seunghyun Note

[프로그래머스] - 카드 뭉치 with JAVA 본문

코딩테스트/백준

[프로그래머스] - 카드 뭉치 with JAVA

승숭슝현 2024. 1. 6. 21:42

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/159994

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 풀이

문제는 간단한 for문 내에 각 요소별로 있는지 확인을 하면 된다.

class Solution {
	public String solution(String[] cards1, String[] cards2, String[] goal) {
		int index1 = 0;
		int index2 = 0;

		for (String str : goal) {
			if (index1 < cards1.length && str.equals(cards1[index1]))
				index1++;
			else if (index2 < cards2.length && str.equals(cards2[index2]))
				index2++;
			else
				return "No";

		}
		return "Yes";
	}
}

 

이 문제를 해결할 때 이상했떤 부분은 이 부분이다.

if (index1 < cards1.length && str.equals(cards1[index1])) index1++;
else if (index2 < cards2.length && str.equals(cards2[index2])) index2++;

조건식 내에 위치가 바뀌면 error 가 나오는 현상이다.

생각을 해보니 && 는 앞의 조건이 맞다면 넘어가는 것인데.. 간과했다.

순서상관없이 비교하고 싶다면 && -> &

728x90