At Spotify, a common text-processing task is to quickly identify the first character in a string that appears exactly once. Given a string s, return the index of the first non-repeating character. If no such character exists, return -1.
s-1 if none existsExample 1
s = "leetcode"0'l' appears once and is the first such character.Example 2
s = "loveleetcode"2'v' is the first character that appears exactly once.Example 3
s = "aabb"-11 <= len(s) <= 10^5s contains only lowercase English lettersA correct solution should avoid comparing every character against every other character. Aim for a linear-time approach using an auxiliary data structure for counting.
s = "leetcode"Output0WhyThe character `'l'` appears exactly once and is the first unique character.s = "loveleetcode"Output2WhyThe characters `'l'` and `'o'` repeat, while `'v'` appears once at index 2.s = "aabb"Output-1WhyAll characters repeat, so there is no non-repeating character.1 <= len(s) <= 10^5s contains only lowercase English lettersReturn -1 if no unique character existsdef first_unique_char(s):