U

118. First Non-Repeating Character in String

Easy

Problem Description

Given a string SS, find the first non-repeating character in it and print its 0-based index. If no non-repeating character exists, print -1.

Input Format

A single line containing string SS.

Output Format

Print the 0-based index, or -1.

Example 1:
Input:
leetcode
Output:
0
Explanation: Character "l" at index 0 does not repeat.
Example 2:
Input:
loveleetcode
Output:
2
Explanation: Character "v" at index 2 does not repeat.
Example 3:
Input:
aabb
Output:
-1
Explanation: All characters repeat; output -1.
Constraints:
  • Standard integer bounds: 32-bit signed integer or input range [1, 10^5].
  • Time complexity target: O(N) or O(N log N) where applicable.
  • Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online