+ 4

Python program

Input Specification:- input1: The input string Output Specification:- Return a hashmap containing three most common characters along with their occurrence count. Example 1:- input1: aabbbccde Output: {"b":3,"a":2,"c":2} Explanation: Here, b occurs 3 times. Hence, it occurs first in the hashmap. Both a and c occur 2 times. So, a occurs in second place and cin the third place because a comes before c in the alphabet. Note: The string has at least 3 distinct characters.

14th Oct 2021, 7:48 AM
Manoj Kumar T
Manoj Kumar T - avatar
4 Réponses
+ 1
txt = input() dic = {} for i in txt: if i not in dic: dic[i] = txt.count(i) print(dic)
14th Oct 2021, 8:42 AM
Erlénio.RS
Erlénio.RS - avatar
+ 3
It's really simple! i) take input # 'aajjddbs' ii) using set comprehension take out unique letter from that string. # {a,j,d,b,s} If you want resultant dictionary in alphabetical order, convert that set into list then call shorted function with that list. # [a,b,d,j,s] iii) Now iterate through this list and simultaneously call the count() method of original string 'aajjddbs' (string is object too) and pass that yielded value to it as argument. This will return number of occurrence of every letter in original string. iv) now save this result in dictionary. string_ = input('Enter String..\n') unq = sorted(list({x for x in string_})) result = {} for i in unq: result[i] = string_.count(i) print(result)
14th Oct 2021, 9:07 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 2
Oh that's just a dictionary. Loop through each character and if it's in the dictionary, add one to it's value. If not, add to dictionary with the value 1.
14th Oct 2021, 8:05 AM
Slick
Slick - avatar
+ 1
But output is not correct, it should return hashmap characters with occurance
14th Oct 2021, 9:45 AM
Manoj Kumar T
Manoj Kumar T - avatar