EasyHash TableStringCounting

1 approach, code in every language

Problem description

Given a string, scan it from left to right and find the earliest character that appears exactly once across the whole string. Report the zero-based index of that character.

If every character in the string repeats, no such character exists and you should return -1.

Example 1

Input: s = "swiss"

Output: 1

's' occurs three times, so it is skipped. 'w' at index 1 is the first character that shows up only once.

Example 2

Input: s = "aabbcc"

Output: -1

Every character appears twice, so there is no character that occurs exactly once.

Constraints

  • 1 <= s.length <= 10^5
  • s contains only lowercase English letters

After the pattern

Now solve one you haven't seen.

First Solo Stop is our own problem on the technique behind First Unique Character in a String: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free