Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.The string s consists of only lowercase English letters.

Solution

The most straightforward approach involves using a frequency map to keep track of character counts. We'll perform two passes over the string:1. First Pass (Count Frequencies): Iterate through the string s once to populate a frequency map (e.g., a hash map or an array of size 26 for lowercase English letters). Store the count of each character.2. Second Pass (Find First Unique): Iterate through the string s again from the beginning. For each character, check its count in the frequency map. The first character encountered with a count of 1 is our answer. Its index is the result.3. If, after the second pass, no character with a count of 1 is found, it means all characters repeat, and we should return -1.* Time Complexity: O(N), where N is the length of the string, because we iterate through the string at most twice.* Space Complexity: O(1), as the frequency map will store at most 26 lowercase English letters, which is a constant amount of space.pythonclass Solution: def firstUniqChar(self, s: str) -> int: # Create a frequency map (using a list for efficiency with lowercase English letters) char_counts = [0] * 26 # 'a' to 'z' # First pass: Populate the frequency map for char_code in s: char_counts[ord(char_code) - ord('a')] += 1 # Second pass: Find the first unique character for i in range(len(s)): if char_counts[ord(s[i]) - ord('a')] == 1: return i # If no unique character is found return -1