Solución

@fernando_her85·2/4/2026TypeScript
solution.tsTypeScript
function wordBreak(words: string[], sentence: string): string[] | null {

  let currentWord = '';
  const output:string[] = []
  let wordFound = false

  for(let i = 0; i < sentence.length; i ++) {
    currentWord += sentence[i];
    wordFound = false;

    const howMany = words.filter(word => word.startsWith(currentWord) ).length;
    if ( howMany > 1) continue;
    if ( !words.some( (word) => word === currentWord)) continue;
    

    output.push(currentWord);
    currentWord = '';
    wordFound = true;
  }

  if (!wordFound) return null;
  return output.length === 0 ? null : output;
}

// No modificar: necesario para evaluar el resultado.
export { wordBreak };
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.