Want to count values in a #Python sequence? Use Counter: c = Counter('aaabbca') c # Counter({'a': 4, 'b': 2, 'c': 1}) c.most_common() # [('a', 4), ('b', 2), ('c', 1)] c.most_common(2) # [('a', 4), ('b', 2)] Bonus: Counter inherits from dict, getting its methods + operators.
I think this is good for solving DSA problems. Some questions need to return counts. 💯
I saw the aaabbca string and now I dare you see this video and modify your examples accordingly :) https://www.youtube.com/watch?v=PCzkaEhz9bI