First Non-Repeating Word
Python
Medium
2 views
Problem Description
Read n words in order. Output the first word that appears exactly once. If none, output NONE.
Input Format
First line n. Next n lines: word.
Output Format
One word or NONE.
Sample Test Case
Input:
5
bat
apple
bat
cat
apple
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
arr=[]
mp={}
for i in range(1,1+n):
w=(lines[i] if i<len(lines) else '').strip()
arr.append(w)
mp[w]=mp.get(w,0)+1
ans='NONE'
for w in arr:
if mp.get(w,0)==1:
ans=w
break
sys.stdout.write(ans)
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!