Ir para o conteúdo

CódigoNão tentada

Maior prefixo comum

Dificuldade: fácil

Retorne o maior prefixo compartilhado por todas as strings (string vazia se não houver).

Ver solução de referência
// Start with the first word and cut letters off the end until every word starts with it.
export function longestCommonPrefix(strs: string[]): string {
if (strs.length === 0) return '';
let prefix = strs[0];
for (let i = 1; i < strs.length; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = prefix.slice(0, -1);
if (prefix === '') return '';
}
}
return prefix;
}
Carregando editor…