219. Python Program to Find the First Character That Does Not Repeat
HardPhase 3 · Challenge
This one takes a few steps with tools you know: a for loop over a string, count() and break. Try a short word on paper first.
Overview
Finding the first character that does not repeat is a common interview question about strings. It tests how carefully you count and search.
Problem Statement
Ingrid makes word puzzles. In each word, the players must find the first letter that appears only once.
Read a word. Look at its characters from left to right. Find the first character that appears exactly once in the whole word.
- Case matters:
Aandaare different characters. So inAa, each one appears once. - If every character appears more than once, there is no answer.
Print First character that does not repeat: <character> or Every character repeats.
Examples
Example 1. Input swiss gives:
First character that does not repeat: w
Why: s appears 3 times. w and i appear once each, and w comes first.
Example 2. Input abab gives:
Every character repeats
Why: a appears twice, and b appears twice too.
Input
One line with one word: letters only, no spaces, 1 letter or more. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line: First character that does not repeat:, a space and the character, or Every character repeats. The tests try 10 different inputs, also words with capital letters.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.