Facebook Pixel

Amazon Online Assessment 2021 (OA) - Unique Character

There's an interesting experiment currently underway with a team that's working on enhancing the process of search queries; this depends on the placement area of the first unique character within a given search. An aspect of this experiment involves conducting tests to check how well the newly created queries are performing. Talking about a unique character, it can be defined as a character that only pops up once in a string; these characters are solely limited to lowercase English alphabets. To get into the specifics, the algorithm needs to identify and return the position of the first unique character appearing in a string; it's worth noting that this uses 1-based indexing. If a situation arises where there are no unique characters at all within a string, the system should return -1.

Relevant Amazon OA Problems:

Input

  • s: a string

Output

either the 1-based index or -1

Examples

Example 1:

Input:

s = statistics

Output: 3

Explanation:

The unique character are a, c among which a occurs first. Using 1-based indexing, it is at index 3.

Constraints

  • 1 <= length of s <= 10^5
  • The string s consists of lowercase English letters

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro