Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 캐치카페
- 데이콘
- 더현대서울 맛집
- 프로그래머스 파이썬
- 편스토랑 우승상품
- 금융문자분석경진대회
- SW Expert Academy
- ubuntu
- programmers
- gs25
- 편스토랑
- leetcode
- Baekjoon
- 우분투
- 자연어처리
- 프로그래머스
- Kaggle
- PYTHON
- Real or Not? NLP with Disaster Tweets
- AI 경진대회
- Git
- 파이썬
- Docker
- 맥북
- 코로나19
- dacon
- github
- ChatGPT
- hackerrank
- 백준
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 451. Sort Characters By Frequency (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 451. Sort Characters By Frequency (Python)
솜씨좋은장씨 2020. 10. 20. 00:01728x90
반응형
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
Solution
from collections import Counter
class Solution:
def frequencySort(self, s: str) -> str:
answer = ""
cnt_items = Counter(list(s)).most_common()
for item in cnt_items:
answer = answer + item[0] * item[1]
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 206. Reverse Linked List (Python) (0) | 2020.10.22 |
---|---|
[leetCode] 1078. Occurrences After Bigram (Python) (0) | 2020.10.21 |
[leetCode] 215. Kth Largest Element in an Array (Python) (0) | 2020.10.19 |
[leetCode] 347. Top K Frequent Elements (Python) (0) | 2020.10.14 |
[leetCode] 442. Find All Duplicates in an Array (Python) (0) | 2020.10.13 |
Comments