728x90
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 풀이
이 문제는 cards1과 cards2의 순서에 맞게 goal이 나오는지 확인하면 된다. 순서가 뒤바뀐 경우, "No"를 반환한다.
정답 코드
C++
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> cards1, vector<string> cards2, vector<string> goal) {
int i = 0, j = 0;
while (i + j < goal.size()) {
if (i < cards1.size() && cards1[i] == goal[i + j])
i++;
else if (j < cards2.size() && cards2[j] == goal[i + j])
j++;
else return "No";
}
return "Yes";
}
python
def solution(cards1, cards2, goal):
i = 0
j = 0
while i + j < len(goal) :
if i < len(cards1) and cards1[i] == goal[i + j] :
i += 1
elif j < len(cards2) and cards2[j] == goal[i + j] :
j += 1
else :
return "No"
return "Yes"
728x90
'Algorithm > PS' 카테고리의 다른 글
[C++, python] 프로그래머스 - 햄버거 만들기 (0) | 2023.02.26 |
---|---|
[C++, python] 프로그래머스 - 성격 유형 검사하기 (1) | 2023.02.26 |
[C++, python] 프로그래머스 - 문자열 나누기 (0) | 2023.02.26 |
[C++, python] 프로그래머스 - 옹알이 (2) (0) | 2023.02.25 |
[C++, python] 프로그래머스 - 기사단원의 무기 (0) | 2023.02.25 |