Skip to content
CodeKruPro

First non-repeating character

Coding problemEasyAsked in: Amazon, Flipkart, Goldman Sachs, MakeMyTrip, Microsoft, Ola Cabs

Given a string s, find the first character that appears exactly once in the whole string, and return its position (counting from 0). If every character repeats, return -1.

Upper and lower case are different characters, and spaces count like any other character.

Examples

Example 1
Input: s = "swiss"
Output: 1
w is the first character that occurs only once.
Example 2
Input: s = "aabb"
Output: -1
Example 3
Input: s = "Aa"
Output: 0

Constraints

  • The length of s is between 0 and 100,000
  • s holds printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.