Menu
CoddyTech

First Unique Character in a String

EasyHashingpython iconjava iconcpp iconc iconjs icon+10

You get a string s of lowercase English letters. Find the first character that appears exactly once in the whole string and return its index, counting from 0. If every character appears more than once, return -1.

Function

firstUniqChar(s: string) → integer
sstring
the string to search, lowercase letters only
Returnsinteger
the index of the first letter that appears exactly once, or -1 if there is none

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s contains only lowercase English letters (a to z).

Examples

Input
s = "coddycode"
Output
4
Explanation
In coddycode, c and o appear twice and d three times. Both y (index 4) and e (index 8) appear once, and y comes first, so the answer is 4.
challenge icon

Follow-up

Characters arrive one at a time from a stream. How would you report the first unique character so far after each one?

Reset code
def firstUniqChar(s):
    # Write code here

Test #1

Test #2

Test #3

Argument

s = "coddycode"

Expected Output

4