LeetCode 3 Longest Substring Without Repeating Characters

Victoria
2 min readJun 10, 2021

This, again, is considered a sliding window problem

Did not solve this problem on my own. The logic of this question goes like this: In order to find substring without repeating characters, we just put them into a HashSet and we have 2 pointers point to the beginning.

If the HashSet does not contain the character the right pointer is pointing to, then add it to the HashSet. And then we make right++. Also, we update the max. We compare currently how big the size of the HashSet is with the old max.

If the HashSet does contain the character already, we know that we have encountered a repeated character.

(for example, left is pointing to a and right is pointing to the second a)

a b c a b c b b

At this moment, a is already in the HashSet. What we do is that we simply remove a from the HashSet, and then make left++. Therefore, currently our HashSet has b and c. We do not need to clean up the whole HashSet. We can just make right pointer keep going to see how far(long) we can go this time.

A few syntax things to notice:

HashSet is like a HashMap but only with the keys.

to get a character of a string we use string.charAt(index)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Victoria
Victoria

Written by Victoria

"What if I fall?" "Oh my darling, but what if you fly?"

No responses yet

Write a response