Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b4880c

Browse files
authored
Update Interview Questions.md
1 parent 5728374 commit 0b4880c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎Coding Interview Prep/Interview Questions.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,56 @@ const throttleFunc = (func, interval) => {
207207
* Implement the Promise.all function
208208
* Flatten a nested JavaScript array without using Array.prototype.flat()
209209
* Implement a custom Sort() function
210+
211+
* To find the **longest substring without repeating characters**, you can use a **sliding window** approach with two pointers (left and right) to track the current window, and a hash map to store the index of the characters.
212+
213+
Here is the solution in JavaScript:
214+
215+
```javascript
216+
function lengthOfLongestSubstring(s) {
217+
let seen = new Map(); // Hash map to store the last index of each character
218+
let maxLength = 0;
219+
let left = 0; // Left pointer for the sliding window
220+
221+
for (let right = 0; right < s.length; right++) {
222+
const currentChar = s[right];
223+
224+
// If the character is already in the map and its index is greater than or equal to left, update left
225+
if (seen.has(currentChar) && seen.get(currentChar) >= left) {
226+
left = seen.get(currentChar) + 1;
227+
}
228+
229+
// Update or add the current character's index in the map
230+
seen.set(currentChar, right);
231+
232+
// Calculate the maximum length of substring so far
233+
maxLength = Math.max(maxLength, right - left + 1);
234+
}
235+
236+
return maxLength;
237+
}
238+
```
239+
240+
### Example Usage:
241+
242+
```javascript
243+
console.log(lengthOfLongestSubstring("abcabcbb")); // Output: 3 ("abc")
244+
console.log(lengthOfLongestSubstring("bbbbb")); // Output: 1 ("b")
245+
console.log(lengthOfLongestSubstring("pwwkew")); // Output: 3 ("wke")
246+
console.log(lengthOfLongestSubstring("")); // Output: 0
247+
```
248+
249+
### Explanation:
250+
1. **Sliding Window**:
251+
- Use two pointers (`left` and `right`) to maintain the current window of characters.
252+
- As you move `right` through the string, check if the current character is already in the map and its last index is within the window (i.e., greater than or equal to `left`).
253+
254+
2. **Updating Window**:
255+
- If a repeating character is found, move the `left` pointer just after the previous occurrence of that character.
256+
- Store or update the index of each character in the hash map (`seen`).
257+
258+
3. **Calculate Length**:
259+
- For each new character, update the maximum length of the substring if the current window is longer.
260+
261+
### Time Complexity:
262+
- **O(n)** where `n` is the length of the string. Each character is processed at most twice (once when the right pointer moves and once when the left pointer moves).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /