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 the letters c and o appear twice, d three times and e once, at index 8. But y also appears once, at index 4, and it comes first, so the answer is 4.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

Characters arrive one at a time from a stream, and after each one you must report the first unique character so far. How would you keep the answer up to date?

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

Case 1

Case 2

Case 3

Input

s = "coddycode"

Expected

4