First Unique Character in a String
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
This problem is one of the competitive questions dealing with strings, so as indicated by the title we need to find first unique i.e non-repeating character in a given string and return the index or else if no such character exists we return -1.
Note: We are considering only lowercase, if asked with uppercase or camelcase full string can be converted to lowercase.
Example :
Input : opengenus
Output : 0
# unique character ' O ' at position zero
Input : hellhell
Output : -1
# no unique character all are repeated
Solution Approach
So the solution for this problem can be obtained by counting the occurrence of each character in a string and we are looking for unique characters so it must occur only once in the whole string and even if there is more than one of such characters we need the first one.
Steps
- Iterate through each character in the string
- Flag/note each occurance of the characters
- Next, we find out which character occurs only once and then return it's position
- Else, if there are no unique characters we return -1
Explanation
-
Iteration through the whole string can be done with the help of a simple for loop till the end i.e size/length of the string.
-
There are 26 alphabets and each needs to be flagged or counted the no.of time they occur, this can be done with an array. ASCII values of
a - z
lie between97 - 122
so if wesubtract 97
from it we get the index value of the letter like 0 for a and 1 for b and so on. Now we will flag them by incrementing the array at each poition of occurance like if the string isaa
then consider an arrayarr[]
to flag them and s is the string and i is iteratorin loop then,arr[s[i]-97]
gives firsts[i]
isa
, thena-97
results in zero thenarr[0]++;
is flagged thus more the occurances more the value. -
Now, we can iterate through the array
arr
and find which occurs only once else we return -1.
Pseudocode
- Start
- Read the input string of letters from the user
- Create an [flag array]array of size 26 for each alphabet
- Initialize all the values of flag array to zero
- Iterate through each letter in the string
- At each iteration for every occurance of an alphabet increase the flag value
- The postion of flag is based on the alpahbet (a=1, b=2...z=26)
- After complete iteration find the first flag value = 1 i.e the alphabet appers only and print it
- If there is no such value return -1
- Exit
Program in C++
Following is the C++ implementation:
#include <iostream>
using namespace std;
int firstUniqueChar(string s) {
int j;
int a[26]={0};
for(j=0;j<s.size();j++)
a[s[j]-97]+=1;
for(j=0;j<s.size();j++)
{
if(a[s[j]-97]==1)
return j;
}
return -1;
}
int main()
{
string s;
cin>>s;
int ans;
ans=firstUniqueChar(s);
cout<<ans;
return 0;
}
Example Step by Step Explaination
Input : opengenus
string=opengenus
- First we create an array flag with 26 values initialized to zero
- Each postion represent an alphabet so the array represents
a b c d .. z
0 0 0 0 .. 0
- Now we iterate through each letter of the word and increase the flag values
o p e n g e n u s
flag[string[position]-97] => flag[(ascii value)-97] => flag[pos]++
- ' o ' flag[string[0]-97] => flag[111-97] => flag[14]++ (incremented) flag[14]=1;
- ' p ' flag[string[1]-97] => flag[112-97] => flag[15]++ (incremented) flag[15]=1;
- ' e ' flag[string[2]-97] => flag[101-97] => flag[04]++ (incremented) flag[04]=1;
- ' n ' flag[string[3]-97] => flag[110-97] => flag[13]++ (incremented) flag[13]=1;
- ' g ' flag[string[4]-97] => flag[103-97] => flag[06]++ (incremented) flag[06]=1;
- ' e ' flag[string[5]-97] => flag[101-97] => flag[04]++ (incremented) flag[04]=2;
- ' n ' flag[string[6]-97] => flag[110-97] => flag[13]++ (incremented) flag[13]=2;
- ' u ' flag[string[7]-97] => flag[117-97] => flag[20]++ (incremented) flag[20]=1;
- ' s ' flag[string[8]-97] => flag[115-97] => flag[18]++ (incremented) flag[18]=1;
-
Now from the beginning we look for values in flag = 1
This means that the alphabet has occured only once. -
So we get flag[14]=1 so the 14th letter ' o ' is our answer
-
OUTPUT : o [First Unique Character in the given String]
Thoughts and different approaches
There might be many other ways to solve this problem but in this approach I tried to explain the logic as simple as possible, we can use this logic in similar scenarios and keep exploring and learn new methods. The above method is called as hashmap method for this types of problem we can also try other ways like using dictionaries in Python or using minimum index value with Java and any other languages.
With this article at OpenGenus, you must have the complete idea of the algorithm to find the First Unique Character in a String. Enjoy.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.