Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)
What might be the best space and time-efficient productive answer for finding the primary non-repeating character for a string like aabccbdcbe?

1 Answer

0 votes
by (26.4k points)

The solution is, O(n):

def fn(s):

  order = []

  counts = {}

  for x in s:

    if x in counts:

      counts[x] += 1


      counts[x] = 1 


  for x in order:

    if counts[x] == 1:

      return x

  return None

We actually loop through the string once. At the point when we go over another character, we store it in checks with an estimation of 1, and add it to order. At the point when we run over a character we've seen previously, we increase its value in counts. At last, we loop through order until we discover a character with an estimation of 1 in checks and returns it back.

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

To know more about this you can have a look at the following video tutorial:-

Browse Categories