Loading content...
You are given a string s consisting of lowercase English letters. Your task is to identify the first character in the string that appears exactly once (i.e., has no duplicates anywhere else in the string) and return its 0-based index position.
If every character in the string repeats at least once, meaning no singular (non-repeating) character exists, return -1.
A character is considered singular if and only if its frequency throughout the entire string is exactly one. The goal is to find the earliest occurring such character based on its position in the string.
s = "leetcode"0The frequency of each character is: 'l'→1, 'e'→3, 't'→1, 'c'→1, 'o'→1, 'd'→1. Characters 'l', 't', 'c', 'o', and 'd' all appear exactly once. Among these, 'l' appears first at index 0, so we return 0.
s = "loveleetcode"2Analyzing the string: 'l'→2, 'o'→2, 'v'→1, 'e'→4, 't'→1, 'c'→1, 'd'→1. The singular characters are 'v' (index 2), 't' (index 6), 'c' (index 8), and 'd' (index 10). The earliest singular character is 'v' at index 2.
s = "aabb"-1Character 'a' appears twice and 'b' appears twice. Since there is no character with a frequency of exactly 1, we return -1.
Constraints