Introduction

The First Unique Character in a String problem involves finding the first non-repeating character in a string.

Given a string s, the task is to:

  • find the first character that appears only once
  • return its index

If no unique character exists:

  • return -1

This problem helps in understanding:

  • hashing
  • frequency counting
  • string traversal
  • efficient searching

Example

Input:s = "leetcode"

Output:
0
Explanation:
'l' appears only once
and is the first unique character.
Input:
s = "aabb"
Output:
-1

Explanation:
All characters repeat.
No unique character exists.

Constraints

 1 <= s.length <= 10^5s contains lowercase English letters

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Traverse every character
  2. Count its frequency
  3. Return index if frequency becomes 1

This approach works but repeated counting increases time complexity.

Steps

  1. Traverse string characters.
  2. Count frequency of current character.
  3. If frequency becomes 1:
    • return index
  4. Continue traversal otherwise.
  5. Return -1 if no unique character exists.

Dry Run

Input:s = "leetcode"
Check l: Frequency = 1
Unique character found
Index = 0
Final Result:
0

Brute Force Code

Complexity Analysis

Time Complexity: O(n²)Explanation:
Nested traversal is used for frequency counting.
Space Complexity: O(1)
Explanation:
No extra data structures are used.

Approach 2 : Optimized Solution (Hashing)

Explanation

The optimized solution uses frequency counting with hashing.

The idea is:

  1. Count frequency of every character
  2. Traverse string again
  3. Return first character having frequency 1

This avoids repeated counting.

Steps

  1. Create frequency hashmap.
  2. Count character frequencies.
  3. Traverse string again.
  4. Return first index having frequency 1.
  5. Return -1 otherwise.

Dry Run

Input:s = "leetcode"

Store frequencies:
l → 1
e → 3
t → 1
c → 1
o → 1
d → 1
Traverse string:
l has frequency 1
Return index 0

Final Result:
0

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
String is traversed twice.

Space Complexity: O(n)
Explanation: Extra hashmap is used for frequencies.

Edge Cases

  1. All characters repeat
  2. First character unique
  3. Last character unique
  4. String contains one character
  5. Large input size

Why This Problem is Important

This problem helps in understanding:

  1. Hashing
  2. Frequency counting
  3. Efficient searching
  4. String traversal
  5. Duplicate handling

It is one of the most common hashing interview problems.

Real-World Applications

Unique character detection is used in:

  1. Data validation
  2. Compiler design
  3. Search engines
  4. Pattern matching
  5. Text analytics systems

Common Mistakes

  1. Incorrect frequency counting
  2. Returning character instead of index
  3. Forgetting second traversal
  4. Missing no-answer case

Interview Tips

Interviewers often expect:

  1. Hashing optimization
  2. Proper frequency logic
  3. O(n) solution explanation

Always explain why second traversal preserves original order.

Related Questions

  1. Valid Anagram
  2. Frequency Count
  3. Ransom Note
  4. Character Frequency
  5. Single Number

Final Takeaway

The First Unique Character in a String problem is a fundamental hashing problem that teaches frequency counting and efficient lookup techniques. Understanding this problem builds a strong foundation for advanced hashing and string processing interview problems.