Problem of the day
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”.
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.
For each test case, return the index of the first unique character, and if there is no unique character return “-1”.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^4
Time Limit: 1 sec
2
16
codingninjascode
24
practicepracticepractice
6
-1
For the first subtask the explanation is given in the problem statement.
For the second subtask there are no unique characters so ans is -1.
3
19
palindromemordnilap
9
notunique
7
caaabbc
10
2
-1
For the first subtask, every character except ‘e’ occurs 2 times so we print the index of e that is 10.
For the second subtask, the characters ‘o’ , ‘t’ , ‘e’ , ‘i’ and ‘q’ are unique but ‘o’ occurs before all the other unique characters .
For the third subtask, all the characters are not unique so we return -1.