oohyoo 님의 블로그

[Python] 프로그래머스 - 외톨이 알파벳 본문

코딩테스트

[Python] 프로그래머스 - 외톨이 알파벳

oohyoo 2025. 2. 3. 22:46

[문제]

 

[풀이]

def solution(input_string):
    count_dict = {}  
    prev = None  
    
    for ch in input_string:
        if ch != prev:  
            count_dict[ch] = count_dict.get(ch, 0) + 1
        prev = ch  
        print(count_dict)

    lonely_chars = sorted([ch for ch, count in count_dict.items() if count >= 2])

    return "".join(lonely_chars) if lonely_chars else "N"

 

.get() 메서드를 살펴보면, ch의 값이 없다면 0을 반환하겠다는 의미로 사용되었다.

반응형