유형 : 해시

풀이 시간 : 10분

def solution(participant, completion):
    pardict = dict()
    for par in participant:
        if par not in pardict:
            pardict[par] = 1
        else:
            pardict[par] += 1
    
    comdict = dict()
    for com in completion:
        if com not in comdict:
            comdict[com] = 1
        else:
            comdict[com] += 1
    for par in pardict.keys():
        if par not in comdict or comdict[par] != pardict[par]:
            return par

 

dict를 사용해 개수를 세고, 그 개수가 다르거나 dict에 없는 개체를 반환하도록 하였다. 

 

 

* 다른 풀이 : collections 의 counter 사용 

Counter 생성자에 문자열을 인자로 넘기면 각 문자가 문자열에서 몇 번씩 나타나는지를 알려주는 객체가 반환됩니다.

import collections


def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)
    return list(answer.keys())[0]

 

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

49. 숫자 짝꿍 ★ (Counter, union)  (0) 2024.07.16
48. 체육복  (0) 2024.07.16
46. 대충 만든 자판 (+enumerate)  (0) 2024.07.16
45. 둘만의 암호  (0) 2024.07.16
44. 로또의 최고 순위와 최저 순위  (0) 2024.07.16
복사했습니다!