Finding the First Unique Character in a String: A Practical Guide

Mrinmayee Rane
3 min readMay 20, 2024

Introduction

Identifying the first non-repeating character in a string is a common algorithmic challenge that tests one’s ability to manipulate data structures efficiently. This article will explore how to solve this problem using Python and Java, breaking down the algorithm step-by-step to ensure a clear understanding of the approach.

Problem Statement

Given a string, find the first non-repeating character and return its index. If no such character exists, return -1.

Approach

To tackle this problem, we need a way to count the occurrences of each character and identify the first unique one. Here’s a structured plan to achieve this:

  1. Count Character Occurrences: Use a data structure to count how many times each character appears in the string.
  2. Track Unique Characters: Maintain a list or collection of characters that appear only once.
  3. Iterate Through the String: For each character in the string:
    - If the character has been seen before, increment its count and remove it from the unique list if it exists.
    - If the character is new, add it to the count map and the unique list.
  4. Find the First Unique Character: After processing all characters, check the list of unique characters. The first one in this list is our answer.
  5. Return the Result: If a unique character is found, return its index. If not, return -1.

Algorithm

Let’s delve deeper into the algorithm with pseudo-code:

  1. Initialize a dictionary (hash map) to count occurrences of each character.
  2. Initialize a list to keep track of characters that appear only once.
  3. Loop through each character in the string:
    - If the character is already in the dictionary, increment its count and remove it from the unique list if it is present.
    - If the character is not in the dictionary, add it with a count of one and append it to the unique list.
  4. Check the unique list:
    - If it has elements, return the index of the first character in this list.
    - If it is empty, return -1.

Implementation

Python

Here’s the Python implementation of the described approach:

class Solution:
def firstUniqChar(s: str) -> int:
count_map = {}
unique_chars = []

# Count occurrences of each character
for c in s:
if c in count_map:
count_map[c] += 1
if c in unique_chars:
unique_chars.remove(c)
else:
count_map[c] = 1
unique_chars.append(c)

# Check for the first unique character
if unique_chars:
return s.index(unique_chars[0])
else:
return -1

# Example usage:
if __name__ == "__main__":
s = Solution()
print(s.firstUniqChar("loveleetcode")) # Output: 2

Java

Now, let’s look at the Java implementation:

import java.util.*;

public class FirstUniqChar {
public static int firstUniqChar(String s) {
Map<Character, Integer> map = new HashMap<>();

// Count occurrences of each character
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}

// Find the first unique character
for (int i = 0; i < s.length(); i++) {
char letter = s.charAt(i);
if (map.get(letter) == 1) {
return i;
}
}

return -1;
}

public static void main(String[] args) {
System.out.println(firstUniqChar("loveleetcode")); // Output: 2
}
}

Explanation

  1. Counting Occurrences: Both implementations use a map (dictionary in Python, HashMap in Java) to keep track of how many times each character appears in the string. This allows us to quickly determine the count of any character.
  2. Tracking Unique Characters: In Python, we use a list to track characters that appear only once. When a character’s count exceeds one, it is removed from this list.
  3. Finding the First Unique Character: After building the count map, we iterate through the string again to find the first character with a count of one.

Conclusion

Finding the first unique character in a string can be efficiently solved using a map to count character occurrences and a list to track unique characters. Both the Python and Java implementations provided demonstrate a clear and concise approach to this problem, ensuring that the solution runs in linear time with constant space complexity.

--

--