LeetCode: Longest Substring Without Repeating Characters Solution

Sliding window uses two pointers?

Approach

Use

Set()
for efficient repeating characters check

Implementation

1var lengthOfLongestSubstring = function (s) {
2 const set = new Set()
3 let res = 0
4
5 for (let lo = (hi = 0); hi < s.length; ) {
6 if (set.has(s[hi])) {
7 set.delete(s[lo])
8 lo++
9 } else {
10 set.add(s[hi])
11 hi++
12 res = Math.max(res, set.size)
13 }
14 }
15
16 return res
17}

Comments

Loading comments...

Tags

leetcode

string

hash table

sliding window

two pointers

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Flood Fill

Sep 20, 2021

Recursive DFS

Previous Post

LeetCode: Permutation in String

Sep 18, 2021

Notice the constraints

HoningJS

Search Posts