-
프로그래머스 - 짝지어 제거하기Algorithm 2023. 5. 21. 21:26
문제
풀이
입력받은 문자열 s를 배열로 변환한 뒤 순회하면서 각 요소를 새로운 배열 newArr에 push한다
이 때, newArr의 마지막요소와 배열 s의 현재 요소가 다른 값인 경우에 배열 s의 현재요소를 newArr에 push하고,
같은 값인 경우에는 newArr의 마지막 요소를 pop한다
function solution(s) { let newArr = []; s = s.split(''); for(let i = 0; i < s.length; i++){ // s[i] 값이 newArr배열의 마지막값과 다르다면, newArr 배열에 s[i] push if(s[i] !== newArr[newArr.length-1]) newArr.push(s[i]) // s[i] 값이 newArr배열의 마지막값과 같다면, newArr 배열 마지막 요소 pop else newArr.pop(); } return newArr.length ? 0 : 1; }
'Algorithm' 카테고리의 다른 글
프로그래머스 - 폰켓몬 (0) 2023.05.25 프로그래머스 - 성격 유형 검사하기 (0) 2023.05.23 프로그래머스 - [1차] 비밀지도 (0) 2023.05.20 프로그래머스 - 신규 아이디 추천 (0) 2023.05.15 프로그래머스 - 과일 장수 (1) 2023.05.12