The main idea is to count all the occurring characters in a string. If you have a string like aba, then the result should be {'a': 2, 'b': 1}.
What if the string is empty? Then the result should be empty object literal, {}.
def count(string):
return {char:string.count(char) for char in string}
# Count character for each in string
print(count('aba'))
Training on Count characters in your string | Codewars
No comments :
Post a Comment