Overview

Given a string str containing. The problem is to print the characters along with their frequency and in the given format explained in the examples below.

Input : str = "abacd" 
Output : {"a": 2, "b": 1, "c":1, "d": 1}

In python, we have Counter functions that we can import from collections module

from collections import Counter
string = "abacd"
print(Counter(string))

We can also solve it by simple traversing through the list and keep tracking the count of each character in dictionary

string = "abcbsacfdabc"
output = {}
for i in string:
if i in output:
output[i] += 1
else:
output[i] = 1

print(output)