Skip to the content

The doubled word

Problem

"When he writes in a hurry, he types a word twice," says Inspector Ruiz, holding the ransom note. "That habit gives him away."

Write a function that takes the note and returns the first word that shows up twice in a row, in lowercase, as a string. If there is none, return "". Case doesn't matter: in Nobody nobody knows the word is nobody. Only words in a row count: the dog the has none.

The note has only letters and spaces, no punctuation, and it may have extra spaces at the start, at the end or between words: they aren't words and they change nothing. An empty note, or one made only of spaces, returns "".

Example: bring the money money to the bridge returns money.

Examples

  • One repeated word

    "bring the money money to the bridge" → "money"

  • With a capital letter

    "Nobody nobody must know" → "nobody"

  • Split by another word

    "the house the door" → ""

  • An empty note

    "" → ""

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def double_word(note):
    pass

JavaScript

function doubleWord(note) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →