Skip to content

Commit 9ba7f3d

Browse files
committed
feat(js): add modular solution and documentation for normal challenge 3 - Longest Unique Substring
1 parent 10fc822 commit 9ba7f3d

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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}`);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// longestUniqueSubstring.js - Implementation of longest unique substring using sliding window technique
2+
3+
class LongestUniqueSubstring {
4+
static lengthOfLongestSubstring(s) {
5+
const charIndexMap = new Map();
6+
let left = 0;
7+
let maxLength = 0;
8+
let maxSubstring = "";
9+
10+
for (let right = 0; right < s.length; right++) {
11+
if (charIndexMap.has(s[right]) && charIndexMap.get(s[right]) >= left) {
12+
left = charIndexMap.get(s[right]) + 1;
13+
}
14+
charIndexMap.set(s[right], right);
15+
if (right - left + 1 > maxLength) {
16+
maxLength = right - left + 1;
17+
maxSubstring = s.substring(left, right + 1);
18+
}
19+
}
20+
return { maxLength, maxSubstring };
21+
}
22+
}
23+
24+
export default LongestUniqueSubstring;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Challenge: Given a string, find the length of the longest substring without repeating characters.
3+
Use sliding window techniques to optimize the solution's performance.
4+
*/
5+
6+
// main.js - Example usage of LongestUniqueSubstring
7+
8+
import LongestUniqueSubstring from './longestUniqueSubstring.js';
9+
10+
11+
function main() {
12+
const testString = "abcabcbb";
13+
const { maxLength, maxSubstring } = LongestUniqueSubstring.lengthOfLongestSubstring(testString);
14+
console.log(`Length of longest substring without repeating characters: ${maxLength}`);
15+
console.log(`Longest substring without repeating characters: ${maxSubstring}`);
16+
}
17+
18+
main();

0 commit comments

Comments
 (0)