First Non-Repeating Character
JavaScript
Medium
4 views
Problem Description
Given a lowercase string, find the first character that does not repeat. Print the character, or -1 if all repeat.
Input Format
One line string (lowercase).
Output Format
One character or -1.
Constraints
Length up to 2e5.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s){process.stdout.write('-1');process.exit(0);}const freq=new Map();for(const ch of s)freq.set(ch,(freq.get(ch)||0)+1);let ans='-1';for(const ch of s){if(freq.get(ch)===1){ans=ch;break;}}process.stdout.write(ans);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!