Solución

@hectoraguero·8/5/2026TypeScript
solution.tsTypeScript
function editDistance(source: string, target: string): number {
  if (source === target) return 0;
  if (target.trim() === "") return source.length;

  let distance = 0;
  let isSourceLarger = source.length > target.length;
  let big = isSourceLarger ? source : target;
  let short = isSourceLarger ? target : source;
  let bigList = isSourceLarger ? source.split('') : target.split("");
  let shortList = isSourceLarger ? target.split('') : source.split("");

  if(big.includes(short)) return big.replace(short, "").length;
  
  const hasDifferentLengths = bigList.length !== shortList.length;
  let previousComparedChar: string | undefined = "";

  for (const [index, char] of bigList.entries()) {
    let comparedChar = shortList.at(index);

    if (char === comparedChar) {
      previousComparedChar = "";
      continue;
    }

    if (hasDifferentLengths && char === previousComparedChar) {
      previousComparedChar = comparedChar;
      continue;
    }

    distance += 1;
    previousComparedChar = comparedChar;
  }
  return distance;
}

console.log(editDistance('xyabc', 'abc'));
console.log(editDistance('xaybzc', 'abc'));
// No modificar: necesario para evaluar el resultado.
export { editDistance };
1respuestas
Respuestas
@hectoragueroAutor8/5/2026

esta cubre estos tests

console.log(editDistance('yabc', 'abc'));
console.log(editDistance('xyabc', 'abc'));

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.