:orphan: First Unique Character in a String ================================== .. highlight:: none Problem ------- https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string ``s``, find the **first** non-repeating character in it and return its index. If it **does not** exist, return ``-1``.   **Example 1:** .. container:: example-block **Input:** s = "leetcode" **Output:** 0 **Explanation:** The character ``'l'`` at index 0 is the first character that does not occur at any other index. **Example 2:** .. container:: example-block **Input:** s = "loveleetcode" **Output:** 2 **Example 3:** .. container:: example-block **Input:** s = "aabb" **Output:** -1   **Constraints:** - ``1 <= s.length <= 10``\ :sup:```5``` - ``s`` consists of only lowercase English letters. .. highlight:: python Pattern ------- Hash Table, String, Queue, Counting Solution -------- Use a dictionary to count the number of times each character appears in ``s``. Iterate through the string again. The first character that has a count of 1 is the first unique character. Code ---- .. literalinclude:: ../problems/easy/first-unique-character-in-a-string/first_unique_character_in_a_string__approach_1.py :language: python :lines: 12- Test ---- >>> from first_unique_character_in_a_string__approach_1 import firstUniqChar >>> firstUniqChar('leetcode') 0 >>> firstUniqChar('loveleetcode') 2 >>> firstUniqChar('aabb') -1 Complexity ---------- | :math:`n` is the length of the input string. | Time: :math:`O(n)` — 2 passes through the string | Auxiliary Space: :math:`O(n)` — dictionary stores at most :math:`n` key-value pairs .. autofunction:: first_unique_character_in_a_string__approach_1.firstUniqChar