Challenge: First Unique Letter
Completed
Read a word and print the first letter that appears only once, or none.
Input
One word (lowercase letters).
Output
A letter, or none.
Example
Input
leetcode
Output
l
Editor
C++
Hints
First count every letter.
Then walk the word again and stop at the first letter with count 1.
Solution explanation
Two passes: O(n).
Show solution
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int count[26] = {0};
for (char c : s) count[c - 'a']++;
for (char c : s) {
if (count[c - 'a'] == 1) {
cout << c << endl;
return 0;
}
}
cout << "none" << endl;
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module