코딩테스트
LeetCode - 451. Sort Characters By Frequency
yolang
2024. 6. 19. 22:14
728x90

🔗 451. Sort Characters By Frequency
Sorting 문제였다! 쉬었다!
야호
빈도 수 에 따라 출력하면 된다.
tree 라면 e가 2번이여서 eetr 또는 eert가 가능하다.
dictionary에 저장하고 정렬한 후 출력해줬다.
import operator
class Solution:
def frequencySort(self, s):
dictionary = {}
for w in s:
try:
dictionary[w] += 1
except KeyError:
dictionary[w] = 1
di = sorted(list(dictionary.items()), key=operator.itemgetter(1))
answer = ""
for element in di:
w, count = element
for _ in range(count):
answer = w + answer
return answer728x90