First Unique Character in a String
Practice this coding question on Coding Gurukul.
Problem statement
A workshop records each station visit as a lowercase letter in a route log. A station visited exactly once can host a follow-up activity without repeating an earlier stop. The organizer wants the earliest such visit in the original route, so sorting the letters would lose the information needed to choose it. Your task Given the complete log s, print the zero-based index of its first character whose total frequency is one. Print -1 if every character repeats. A character that looks unique in an early prefix may occur again later; use the entire log. Choose by position, not alphabetical order. Worked example For s = swiss, s occurs three times, while w and i each occur once. The earlier unique character is w at index 1, so print 1. For aabb, neither letter is unique, so print -1.
Input format
One nonempty string s on a single line.
Output format
Print one integer: the earliest unique character index, or -1.
Constraints
1 <= length of s <= 100000; s contains only lowercase English letters.
Sample input
swiss
Sample output
1
Sign in to run or submit a solution in C++, Java or Python.