PIXELBANKv8.2.1
Menu

First Unique Character in a String

Given a string s, find the first non-repeating character and return its index. Return -1 if no such character exists.

Example:

Input:
leetcode
Output:
0
Reasoning:
  • First, we count the frequency of each character in the string s = leetcode.
  • Then, we iterate over the string to find the first character with a frequency of 11.
  • We find that the character l appears only once and it is the first character in the string, so its index is 00.
  • The final output is the index of the first non-repeating character, which is 00.

Constraints:

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters
Editor

Test Results

0/0
Run code to see test results.