First lonely letter
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A letter is lonely if it appears exactly once in text.
Return the position of the first lonely letter, reading from the left. If no letter is lonely, return -1.
Positions start at 0.
Examples
Example 1
- Input
- text = "swiss"
- Output
- 1
s appears 3 times, but w (position 1) appears only once.
Example 2
- Input
- text = "level"
- Output
- 2
l and e each appear twice. v, at position 2, is lonely.
Example 3
- Input
- text = "aabb"
- Output
- -1
Every letter appears twice.
Rules
textuses only lowercase letters a to z.texthas between 0 and 110,000 letters.
Words on this page, in plain English (1)Open if any word is new to you
- Index (position)
- Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
A letter is lonely if it appears exactly once in the whole text. We want the position of the first such letter, or -1 if there isn't one.
2. Solve an example by hand
In swiss: s appears three times. w appears once. So the answer is w's position, 1. Notice that you only knew the first s wasn't lonely because you had seen the whole word: its repeats come later.
3. The slow way
For each letter, count how often it appears by scanning the whole text, and return the first one that appears once.
function firstLonelyLetter(text) { for (let i = 0; i < text.length; i++) { let count = 0; for (const letter of text) { if (letter === text[i]) count++; } if (count === 1) return i; } return -1;}Each letter scans the whole text again: O(n²). For 100,000 letters that's up to 10 billion steps.
4. Spot the pattern
We keep asking "how many times does this letter appear?". Instead of recounting for every position, count everything once, up front. That's the count, then answer template.
5. The faster idea
Two passes over the text:
- Count: go through the text and count every letter in a Map.
- Search: go through the text again, in order, and return the first position whose letter has a count of 1.
Count first, then search
text
- pass =
- –
counts (Set)
empty
function firstLonelyLetter(text) { const counts = new Map(); // letter → how many times it appears for (const letter of text) { counts.set(letter, (counts.get(letter) ?? 0) + 1); } for (let i = 0; i < text.length; i++) { if (counts.get(text[i]) === 1) { return i; } } return -1;}Pass 1: count every letter. Start with an empty Map.
Why two passes and not one? During the first pass you can't know whether a letter will turn up again later. After it, you know everything.
6. The code, line by line
const counts = new Map(); // letter → how many times it appears for (const letter of text) { counts.set(letter, (counts.get(letter) ?? 0) + 1); }Pass 1: the counting template. After this loop,
countsknows how many times every letter appears.for (let i = 0; i < text.length; i++) { if (counts.get(text[i]) === 1) { return i; } }Pass 2: walk the text in order, so the first match is the leftmost one.
text[i]is the letter at positioni; if its count is 1, it's lonely.return -1;No lonely letter anywhere.
7. How fast is it?
| Recount for every letter | Count once | |
|---|---|---|
| Time | O(n²) | O(n): two passes is still O(n) |
| Extra memory | O(1) | O(1): at most 26 letters |
Two passes take twice as long as one, but that's a fixed factor: doubling the text still only doubles the work. Big O ignores fixed factors, so O(2n) is just O(n).
Common mistakes
- Returning the letter instead of its position. Return
i, nottext[i]. - Searching the Map in pass 2. A Map remembers the order keys were added, which happens to be the order of first appearance here, but searching the text is clearer and works for any order.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: First lonely letter
Read the 12 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function firstLonelyLetter(text) { const counts = new Map(); // letter → how many times it appears for (const letter of text) { counts.set(letter, (counts.get(letter) ?? 0) + 1); } for (let i = 0; i < text.length; i++) { if (counts.get(text[i]) === 1) { return i; } } return -1;}