Search⌘ K
AI Features

First Unique Character in a String

Explore methods to locate the first unique character in a string by using effective frequency tracking techniques. Understand the problem constraints and develop a clear approach to return the correct index or -1 if no unique character is found. This lesson helps you practice applying frequency analysis for coding interviews.

Statement

For a given string of characters, s, your task is to find the first non-repeating character and return its index. Return 1-1 if there’s no unique character in the given string.

Constraints:

  • 11 \leq s.length 103\leq 10^{3}

  • Only lowercase English letters are accepted.

  • There are no spaces in the string.

Examples

canvasAnimation-image
1 / 3

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

First Unique Character in a String

1.

What is the correct answer if the following string is given as input?

s = “awsjawuh”

A.

2

B.

4

C.

3

D.

6


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > main.cpp
int FirstUniqueChar(string s)
{
// Replace this placeholder return statement with your code
return -1;
}
First Unique Character in a String