LeetCode Longest Substring Without Repeating Characters Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

A very important correction. Not in the code, but in the explanation: We use Set NOT because it stores unique characters, but because (in HashSet) the operation 'contains()' runs in O(1). We really don't need a data structure that maintains unique elements because we are doing that part explicitly using the 'contains()' operation. So the code would run fine even with a regular List. But 'contains()' in List is typically O(n). Hence the choice of (Hash)Set

arunsp
Автор

you just taught a 46 year old, non CS grad, how to handle this problem.
thank you.

This is more to the point than the solution in leetcode.

Keep up the good work.

MrMakemusicmike
Автор

Thank you man... I was smashing my head over this problem for more than 3 hours

utkarshpant
Автор

Instead of incrementing "a_pointer" by 1, we actually have to set it to the index at which we last saw the character at "b_pointer". To store this information, we need to use a Map (instead of a Set)

jibinkjose
Автор

you are single-handedly carrying my leetcode journey, thank you so much!

btsforever
Автор

What if we had abcc? This would pop out a and have bcc in the hash set? Very confused why you'd pop from the beginning

TheSmashten
Автор

Crystal clear help man, appreciate it so much I was struggling with this one, just when I thought I had it each time there would be some test failing on submission. You did great explaining exactly what was happening so hats off to you my friend!

ryzurrin
Автор

This code is obviously much readable than the official LeetCode solution, great job dude!

albertgao
Автор

Awesome explanation man!! Just that you missed two test cases: if(s.isEmpty()){return 0;}
if(s.length()==1){return 1;}

adrijaroy
Автор

Sir, I want to let you know, you are the best! I have been stuck on this question for few hours now, going through solutions on LeetCode and trying to make sense. Now, I watched this video and everything makes sense.

rushm
Автор

u keep sating it is easy but it wasnt easy until you showed me.

주모미디어
Автор

suppose string is "bacad", now when b pointer is at index 3 (when a comes 2nd time), this algorithm will remove b from the sliding window leaving string a ""aca" in it and still it has repeating character a.
Please correct me if I am wrong.

dalbirsinghdhillon
Автор

One improvement I would suggest, in the else-clause when the character is found in the set, instead of removing it simply increment both a_pointer and b_pointer. You're going to end up writing the value back in the set next loop iteration anyways, so you can save yourself a cycle.

mattgervasio
Автор

bug : try this input " abcabkabc"
when b_pointer at 7, it will remove a_pointer at 3 . the set will have "abka"

TheKillermob
Автор

I'm watching your videos because i just graduated and I'm preparing for these tough ass interviews. Your videos are nice and helpful. I wish I could do these problems by myself lmfao. You got any tips on how to get better at ACTUALLY coding these problems? I feel like I try and I don't get it and I tend to get frustrated and give up haha

MWaheed
Автор

Question, if a duplicate occurs is it always the character at a_pointer that can be duplicate.
Eg Say if I have "abcdb", my a_pointer is at 0 and b_pointer is checking window.
After 'd', it encounters 'b' however popping character at a_pointer(which is 'a') results 'bcdb' which still has duplicates..??
If duplicate occurs, is it always same character at a_pointer which can be duplicate..??

SK-luci
Автор

Would you say time complexity is O(n) and space complexity is O(1) constant space, because no matter how long the input string can be, at any given time the worst case extra space we're dealing with in the hashset is 26 characters?

fatimaibrahim-biangoro
Автор

While trying the solution on my own, I stumbled across how to eliminate duplicate characters from a string. So, now, thanks to your video I can do both. :)

steeck
Автор

Hey thanks for the explanation. Could you tell me how does it work for this test case: "pbbbyylmkbn"

codingpros
Автор

I might be missing something but suppose you had something like : "abbcd" how would this system detect that there is a repeat. Because it would just see that there is no a repeat and it would just go through?

bossman