goal 에 있는 내용들을 card1 과 card2 에 있는지 각각 index를 늘려가며 순서대로 찾아보고
찾을때마다 complete 라는 값의 카운트를 증가시켜준다.
모든 작업을 완료하고나서 goal 의 사이즈와 complete의 카운트가 같다면 조건에 맞게 문자열을 만들 수 있다는것이 된다.
class Solution {
fun solution(cards1: Array<String>, cards2: Array<String>, goal: Array<String>): String {
var answer: String = ""
var card1Index = 0
var card2Index = 0
var complete = 0
goal.forEach{
if(card1Index <= cards1.size-1 && it == cards1[card1Index]){
card1Index++
complete++
}else if(card2Index <= cards2.size-1 && it == cards2[card2Index]){
card2Index++
complete++
}
}
if(complete == goal.size){
answer = "Yes"
}else{
answer = "No"
}
return answer
}
}
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 - 숫자 변환하기 (0) | 2024.08.14 |
---|---|
프로그래머스 - 둘만의 암호 (0) | 2024.08.13 |
프로그래머스 - 무인도 여행 (0) | 2024.08.13 |
프로그래머스 - 미로탈출 (0) | 2024.08.12 |
프로그래머스 - 혼자서하는 틱택토 (0) | 2024.08.12 |
프로그래머스 - 대충 만든 자판 (0) | 2024.08.11 |
프로그래머스 - 리코쳇 로봇 (0) | 2024.08.11 |
프로그래머스 - 신고 결과 받기 (0) | 2022.03.21 |