Skip to main content
2 of 6
added 209 characters in body
janos
  • 113.1k
  • 15
  • 154
  • 396

Consider a simpler algorithm:

  • Pass 1: Build a map of counts of characters. You can use a simple int array to store this mapping.
  • Pass 2: For each index, if the count of the current character is 1, return the index.
  • If pass 2 terminates without returning, then there are no unique characters, return -1.

This is simple, straightforward logic. The constant number of passes doesn't change the time complexity.

You could do it in a single pass too, for example using a queue to keep track of the ordering of what was seen first, but I would prefer to avoid the added complexity unless there is a compelling reason.

janos
  • 113.1k
  • 15
  • 154
  • 396