First Unique Character in a String
Explore techniques to find the first unique character in a string by tracking character counts and positions. Understand how to implement an efficient solution that returns the index or -1 if none exists, improving your skills in handling tracking problems with strings.
We'll cover the following...
Statement
For a given string of characters, s, your task is to find the first non-repeating character and return its index. Return if there’s no unique character in the given string.
Constraints:
-
s.length -
Only lowercase English letters are accepted.
-
There are no spaces in the string.
Examples
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
What is the correct answer if the following string is given as input?
s = “awsjawuh”
2
4
3
6
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.
Try it yourself
Implement your solution in the following coding playground.
package mainfunc firstUniqueChar(s string) int {// Replace this placeholder return statement with your codereturn -1}