Skip to the content

The coach's shouts

Problem

In the middle of a play there is no time for a full name. Ngozi Okafor, the coach, shouts only the start of each player's name: the shortest one that is not also the start of any other name on the list.

You get names, a list of different, non-empty strings, lowercase and with no spaces, with at least one. Return a list of strings in the same order: the shout for each player. If no start works, because her whole name is the beginning of another one, her shout is the full name. With a single player, the shout is her first letter.

With ["ana", "anabel", "berta", "bruna"]: "ana" and "anabel" share "ana", so "anabel" needs one more letter, "anab"; "ana" sits whole at the start of "anabel", so she gets her full name. "berta" and "bruna" share only the "b": they become "be" and "br". You return ["ana", "anab", "be", "br"].

Examples

  • The example

    ["ana", "anabel", "berta", "bruna"] → ["ana", "anab", "be", "br"]

  • A single player

    ["ngozi"] → ["n"]

  • They all start differently

    ["kiara", "lupita", "mae"] → ["k", "l", "m"]

  • Names inside other names

    ["al", "alba", "albana"] → ["al", "alba", "alban"]

  • The closest one is not next to it

    ["marisol", "martina", "maria", "mar"] → ["maris", "mart", "maria", "mar"]

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

You start with this

Python

def shouts(names):
    pass

JavaScript

function shouts(names) {
}
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 →