Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Python solution using Counter()

mariana.lopez.jaimez

Mar 23, 2024

In Python, we could take advantage of the Counter() class from the 'collections' library to get the frequency of each character instead of doing it manually. Any tips for improving the following candidate solution would be great:

from collections import Counter class Solution: def firstUniqChar(self, s: str) -> int: characters_count = Counter(s) for index, character in enumerate(s): if characters_count[character] == 1: return index return -1

1

0

Comments
Comments

On this page