C++)
#include <string>
#include <vector>
using namespace std;
string solution(vector<string> cards1, vector<string> cards2, vector<string> goal) {
int card1Idx = 0;
int card2Idx = 0;
bool flag = true;
for(auto ch : goal) {
if(card1Idx < cards1.size() && (cards1.at(card1Idx) == ch))
card1Idx++;
else if(card2Idx < cards2.size() && (cards2.at(card2Idx) == ch))
card2Idx++;
else {
flag = false;
break;
}
}
return (flag) ? "Yes" : "No";
}
goal을 한 문자씩 cards1과 cards2와 비교한다.
둘 중 하나라도 일치하는 문자가 있으면 flag = true
없으면 더 이상 찾을 필요가 없으므로 flag = false 이다.
Python)
def solution(cards1, cards2, goal):
flag = True
idx1 = 0
idx2 = 0
for word in goal:
if idx1 < len(cards1) and word == cards1[idx1]:
idx1 += 1
elif idx2 < len(cards2) and word == cards2[idx2]:
idx2 += 1
else:
flag = False
break
return "Yes" if flag else "No"'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 대충 만든 자판 (C++) (0) | 2023.02.27 |
|---|---|
| [Level1] 성격 유형 검사하기 (C++) (0) | 2023.02.26 |
| [Level1] 키패드 누르기 (C++) (0) | 2023.02.24 |
| [Level1] 크레인 인형뽑기 게임 (C++) (0) | 2023.02.23 |
| [Level1] 문자열 나누기 (C++) (0) | 2023.02.22 |