|
| 1 | +# Challenge Description and Solution |
| 2 | + |
| 3 | +## English Version |
| 4 | + |
| 5 | +### Challenge Description |
| 6 | +Given a string, find the length of the longest substring without repeating characters. Use sliding window techniques to optimize the solution's performance. |
| 7 | + |
| 8 | +### Code Explanation |
| 9 | +The solution uses a sliding window approach with two pointers (`left` and `right`) and a map to track the last index of each character. When a repeated character is found within the current window, the left pointer moves to the right of the previous occurrence to maintain a substring without duplicates. |
| 10 | + |
| 11 | +The function returns both the length and the longest substring without repeating characters. |
| 12 | + |
| 13 | +### Relevant Code Snippet |
| 14 | + |
| 15 | +```javascript |
| 16 | +class LongestUniqueSubstring { |
| 17 | + static lengthOfLongestSubstring(s) { |
| 18 | + const charIndexMap = new Map(); |
| 19 | + let left = 0; |
| 20 | + let maxLength = 0; |
| 21 | + let maxSubstring = ""; |
| 22 | + |
| 23 | + for (let right = 0; right < s.length; right++) { |
| 24 | + if (charIndexMap.has(s[right]) && charIndexMap.get(s[right]) >= left) { |
| 25 | + left = charIndexMap.get(s[right]) + 1; |
| 26 | + } |
| 27 | + charIndexMap.set(s[right], right); |
| 28 | + if (right - left + 1 > maxLength) { |
| 29 | + maxLength = right - left + 1; |
| 30 | + maxSubstring = s.substring(left, right + 1); |
| 31 | + } |
| 32 | + } |
| 33 | + return { maxLength, maxSubstring }; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Example Usage |
| 39 | + |
| 40 | +```javascript |
| 41 | +import LongestUniqueSubstring from './longestUniqueSubstring.js'; |
| 42 | + |
| 43 | +const testString = "abcabcbb"; |
| 44 | +const { maxLength, maxSubstring } = LongestUniqueSubstring.lengthOfLongestSubstring(testString); |
| 45 | +console.log(`Length of longest substring without repeating characters: ${maxLength}`); |
| 46 | +console.log(`Longest substring without repeating characters: ${maxSubstring}`); |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Versión en Español |
| 52 | + |
| 53 | +### Descripción del Reto |
| 54 | +Dada una cadena, encuentra la longitud de la subcadena más larga sin caracteres repetidos. Usa técnicas de ventana deslizante para optimizar el rendimiento de la solución. |
| 55 | + |
| 56 | +### Explicación del Código |
| 57 | +La solución utiliza un enfoque de ventana deslizante con dos punteros (`left` y `right`) y un mapa para rastrear el último índice de cada carácter. Cuando se encuentra un carácter repetido dentro de la ventana actual, el puntero izquierdo se mueve a la derecha de la ocurrencia previa para mantener una subcadena sin duplicados. |
| 58 | + |
| 59 | +La función retorna tanto la longitud como la subcadena más larga sin caracteres repetidos. |
| 60 | + |
| 61 | +### Fragmento de Código Relevante |
| 62 | + |
| 63 | +```javascript |
| 64 | +class LongestUniqueSubstring { |
| 65 | + static lengthOfLongestSubstring(s) { |
| 66 | + const charIndexMap = new Map(); |
| 67 | + let left = 0; |
| 68 | + let maxLength = 0; |
| 69 | + let maxSubstring = ""; |
| 70 | + |
| 71 | + for (let right = 0; right < s.length; right++) { |
| 72 | + if (charIndexMap.has(s[right]) && charIndexMap.get(s[right]) >= left) { |
| 73 | + left = charIndexMap.get(s[right]) + 1; |
| 74 | + } |
| 75 | + charIndexMap.set(s[right], right); |
| 76 | + if (right - left + 1 > maxLength) { |
| 77 | + maxLength = right - left + 1; |
| 78 | + maxSubstring = s.substring(left, right + 1); |
| 79 | + } |
| 80 | + } |
| 81 | + return { maxLength, maxSubstring }; |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Ejemplo de Uso |
| 87 | + |
| 88 | +```javascript |
| 89 | +import LongestUniqueSubstring from './longestUniqueSubstring.js'; |
| 90 | + |
| 91 | +const testString = "abcabcbb"; |
| 92 | +const { maxLength, maxSubstring } = LongestUniqueSubstring.lengthOfLongestSubstring(testString); |
| 93 | +console.log(`Longitud de la subcadena más larga sin caracteres repetidos: ${maxLength}`); |
| 94 | +console.log(`Subcadena más larga sin caracteres repetidos: ${maxSubstring}`); |
0 commit comments