본문 바로가기
프로그래머스/lv.1

숫자 짝꿍

by bingual 2024. 1. 26.
반응형

 

https://school.programmers.co.kr/learn/courses/30/lessons/131128

 

프로그래머스

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

programmers.co.kr

문제풀이

from collections import Counter


def solution(X, Y):
    # 각 숫자 등장 횟수 카운트
    x_cnt = Counter(map(int, X))
    y_cnt = Counter(map(int, Y))

    # 숫자를 중복 제거후 내림차 정렬함
    common_keys = sorted(set(x_cnt.keys()) & set(y_cnt.keys()), reverse=True)

    # 짝궁이 존재 하지 않는 다면 -1 하나만 존재 하면 해당 숫자 리턴
    if not common_keys:
        return "-1"
    elif len(common_keys) == 1:
        return str(common_keys[-1])

    # 각 숫자가 등장한 횟수 만큼 숫자를 출력함
    answer = "".join(str(key) * min(x_cnt[key], y_cnt[key]) for key in common_keys)
    return answer

'프로그래머스 > lv.1' 카테고리의 다른 글

대충 만든 자판  (0) 2024.01.27
체육복  (1) 2024.01.26
로또의 최고 순위와 최저 순위  (0) 2024.01.25
다트게임  (1) 2024.01.25
옹알이 (2)  (1) 2024.01.25