First Unique Character in a String
You are given a string S of length N. Your task is to find the index(considering 1-based indexing) of the first unique character present in the string. If there are no unique characters return -1.
Note
A unique character in a string is the character that appears only once in the string. For example, ‘h’, ‘e’, and ‘o’ are the unique characters in the string “hello”.
Input format :
The first line of input contains a single integer T, denoting the number of test cases.
The first line of each test case contains a positive integer N, which represents the length of the string.
The next line of each test case contains a string S.
Output Format :
For each test case, return the index of the first unique character, and if there is no unique character return “-1”.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraint :
1 <= T <= 100
1 <= N <= 10^4
Time Limit: 1 sec
CodingNinjas
author
2y
Approach :
1) Make a count array and initialize all characters by -1, i.e., all considered as absent.
2) Traverse the given string, and the value of count[x] will be the index of ‘x’ if ‘x’ appears onl...read more
CodingNinjas
author
2y
Bruteforce
- Traverse through all the indices one by one and we will try to see if the character at the current index is unique or not.
- For this, if we are at index i, we will traverse through all the ind...read more
CodingNinjas
author
2y
Using Hashmap
- For this approach, we have to use a hash map. We can also use an array to store frequency as the given string contains only lowercase letters so we can map each letter to its position in ...read more
Add answer anonymously...
Top Amdocs Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amdocs Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app