tcs
Online AssessmentQ1
Easy50 MarksNon-Repeating Character in Encrypted Stream
StringsHash Table
Set 1 • 60 min
Problem Statement
Given a string s consisting of lowercase Latin letters, find the first non-repeating character in it and return its index (0-indexed).
If it does not exist, return -1.
Candidate Instructions
- ▸Use frequency array of size 26.
- ▸First pass: count frequencies. Second pass: return first index with frequency 1.
Input Format
- ▸A single line containing string s.
Output Format
- ▸Print the 0-indexed position or -1.
Constraints
- 1 <= s.length <= 10^5
- s consists of only lowercase English letters
Edge Cases
- ▸All characters repeat (returns -1)
- ▸First character itself is unique
Sample Test Cases
Example 1
Input
leetcode
Output
0
Explanation
'l' appears once at index 0 and does not repeat anywhere in the string.
Example 2
Input
loveleetcode
Output
2
Explanation
'v' at index 2 is the first character with frequency 1.
Complexity
TimeO(N)
SpaceO(1) (26 characters)
Expected Approach
Count frequencies in an array of size 26. Iterate through the string again; return the index of the first character whose count is 1. If none, return -1.