Solución
solution.tsTypeScript
function wordBreak(words: string[], sentence: string): string[] | null {
if (words.join() === sentence) return words;
const arr: [number, string][] = [];
for (const word of words) {
const wordIndex = sentence.indexOf(word);
if (wordIndex === -1) return null
if (arr.some(([i]) => i === wordIndex)) return null
arr.push([wordIndex, word]);
}
const result = arr.toSorted(([a], [b]) => a - b).map(([, s]) => s);
return result;
}
// No modificar: necesario para evaluar el resultado.
export { wordBreak };0respuestas