# First Unique Character in a String - Leet Code Solution

September 08, 2020

## Problem Statement

Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.

Example

s = "leetcode"
return 0.

s = "loveleetcode"
return 2.

Note: You may assume the string contains only lowercase English letters.

## Solution Brute Force

Lets take a look at the simple solution.

• Have two loops. first will iterate till length of string
• in second loop, iterate from beginning to end.
• And check if the character is found anywhere
• We can keep track of duplicate found by a boolean flag
• And if during our inner loop, if we found that character is not found. This is our answer

### Code

public int firstUniqChar_bruteforce(String s) {
for (int i=0; i<s.length(); i++) {
boolean unique = true;
for (int j=0; j<s.length(); j++) {
if (i != j && s.charAt(i) == s.charAt(j)) {
unique = false;
break;
}
}

if (unique) {
return i;
}
}

return -1;
}

Its O(n^2)

## Another Solution using a HashMap

• Iterate over string, and keep track of count of each character
• Maintain a HashMap<Character, Integer>
• Now, iterate over string again
• For each character, lookup in our HashMap
• If the count of that character is only 1, this is our answer
• Since, 1 means this character is in the string only 1 times.

### Code

public int firstUniqChar(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i=0; i<s.length(); i++) {
int count = map.getOrDefault(s.charAt(i), 0);
count ++;
map.put(s.charAt(i), count);
}

for (int i=0; i<s.length(); i++) {
if (map.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}

Its O(n)

## Similar Posts

### Rotate Array - Leet Code Solution

Problem Statement Given an array, rotate the array to the right by k steps…

### How to prepare for your next Coding Interview

Here are some tips while preparing for your coding interviews. 1. Do study or…

### Reverse String - Leet Code Solution

Problem Statement Write a function that reverses a string. The input string is…

### How to nail your Coding Interview

Here are some tips while giving your coding interviews. 1. Never try to jump to…

### Max Priority Queue Implementation with Heap Data structure

Max Priority Queue is a data structure which manage a list of keys(values). And…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…