[백준] 20920. 영단어 암기는 괴로워

원숭2·2022년 2월 22일
0

백준

목록 보기
46/54

문제

풀이

  1. for문을 사용하여 m길이 이상인 단어들만 words배열에 넣어줌.
  2. Counter의 most_common함수를 사용하여 빈도순으로 정렬된 list인 res를 return받음.
  3. res배열을 sort함수의 key와 lambda식을 활용하여 문제의 조건에 맞게 설정해준 후 정렬함.

코드

import sys
from collections import Counter

def solution() :
    n, m = map(int, sys.stdin.readline().split())
    words = []
    
    for _ in range(n) :
        tmp = sys.stdin.readline().rstrip()
        if len(tmp) >= m :
            words.append(tmp)
    res = Counter(words).most_common()
    res.sort(key = lambda x : (-x[1], -len(x[0]), x[0]))

    for r in res :
        print(r[0])
        
solution()

0개의 댓글