Last active
February 4, 2022 10:40
-
-
Save Abuton/2d3e9684096a7912bedf9223d4049aab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you can write to stdout for debugging purposes, e.g. | |
# print("this is a debug message") | |
import collections | |
def solution(S): | |
# write your code in Python 3.6 | |
# create an empty dictionary to store the values of S | |
f_dict = collections.defaultdict(int) | |
# loop S | |
for i in S: | |
# increase number of times each character exists in S | |
f_dict[i] += 1 | |
# create an empty list to store the values in f_dict | |
f_list = [] | |
for key, value in f_dict.items(): | |
f_list.append(value) | |
f_list.sort(reverse=True) | |
f_set = set() | |
count = 0 | |
for v in f_list: | |
if v in f_set: | |
while True: | |
v -= 1 | |
count += 1 | |
if v not in f_set: | |
break | |
if v != 0: | |
f_set.add(v) | |
return count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment