First Unique Character in a String

TopicDifficultyCompanies
Hash Table
EASY
Amazon
Microsoft

Given a string s, write a program to find the first non-repeating character in it and return its index.

Problem Note

  • The string index starts from 0.
  • If there is no non-repeating character in the string sreturn -1.
  • You may assume the string s contains only lowercase characters.

Example 1

Input: s = "afteracademy"
Output: 1
Explanation: 'f' is the first non-repeating character in the string s. Hence, the output is index 1.

Example 2

Input: s = "mindorks"
Output: 0
Explanation: 'm' is the first non-repeating character in the string s. Hence, the output is index 0.

Example 3

Input: s = "abacdcd"
Output: -1
Explanation: In the string s, there is no non-repeating character. Hence, the output is -1.

Code Editor

Practice and Learn

Best way to learn is through solving real problems. Practice this problem in this code editor.