Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
La subcadena más larga no funciona para caracteres repetidos

Tengo una función de ventana móvil bastante estándar para encontrar la subcadena más larga.

 var lengthOfLongestSubstring = function (s) { let currentString = []; let longestStringLength = 0; let longestString = []; for (let i = 0; i < s.length; i++) { // Attempt to get the current character's position in the current string const currentCharacterPosition = currentString.indexOf(s[i]); // Check if the current character exists in the current string if (currentCharacterPosition !== -1) { // Chop array of the first occurrence of the character currentString.splice(0, currentCharacterPosition + 1); } // Add the current character to the array currentString.push(s[i]); // Store the current string length if bigger than the existing record if (longestStringLength < currentString.length) { longestStringLength = currentString.length; longestString = currentString; console.log("Inside " + currentString + ", " + longestString + " " + longestStringLength); } console.log("Outside " + currentString + ", " + longestString + " " + longestStringLength); } }; let result = lengthOfLongestSubstring("abcabcbb");

La salida es

 Inside a, a 1 Outside a, a 1 Inside a,b, a,b 2 Outside a,b, a,b 2 Inside a,b,c, a,b,c 3 Outside a,b,c, a,b,c 3 Outside b,c,a, b,c,a 3 Outside c,a,b, c,a,b 3 Outside a,b,c, a,b,c 3 Outside c,b, c,b 3 Outside b, b 3

PROBLEMA: ¿Por qué "longestString" está fuera del segundo ciclo si cambia de valor? no lo actualizo por ningun lado

Probé con lo siguiente y funciona bien. Algunas cuerdas funcionan sin ningún problema. P.ej:

let result = lengthOfLongestSubstring("abcabcbbdfeghj");

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda