Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0387 - First Unique Character in a String
    0387 - First Unique Character in a String
    0387 - First Unique Character in a String
    0387 - First Unique Character in a String

    0387 - First Unique Character in a String

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/first-unique-character-in-a-string/

    Solution 1 - Hash Map (Counter)

    Count the frequency of each character using a hash map. Then iterate through the string again and return the index of the first character with a count of 1. If no unique character exists, return -1.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Array Count (Fixed Size)

    Since the input only contains lowercase English letters, use a fixed-size array of length 26 to count character frequencies. Then iterate through the string and return the index of the first character whose count is 1.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0387.1 - First Unique Character in a String - Solution 1 - Hash Map (Counter) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from collections import Counter
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def firstUniqChar(self, s: str) -> int:
            """First Unique Character in a String Function"""
            count = Counter(s)
    
            for i, c in enumerate(s):
                if count[c] == 1:
                    return i
    
            return -1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().firstUniqChar("leetcode"))       # 0
        print(Solution().firstUniqChar("loveleetcode"))   # 2
        print(Solution().firstUniqChar("aabb"))           # -1
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0387.2 - First Unique Character in a String - Solution 2 - Array Count (Fixed Size) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def firstUniqChar(self, s: str) -> int:
            """First Unique Character in a String Function"""
            count = [0] * 26
    
            for c in s:
                count[ord(c) - ord('a')] += 1
    
            for i, c in enumerate(s):
                if count[ord(c) - ord('a')] == 1:
                    return i
    
            return -1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().firstUniqChar("leetcode"))       # 0
        print(Solution().firstUniqChar("loveleetcode"))   # 2
        print(Solution().firstUniqChar("aabb"))           # -1
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()