LeetCode Longest Substring Without Repeating Characters Solution Explained - Java

  Рет қаралды 203,897

Nick White

Nick White

Күн бұрын

Пікірлер: 262
@arunsp767
@arunsp767 2 жыл бұрын
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
@tahaansari5621
@tahaansari5621 2 жыл бұрын
I see u going on every video and commenting out the same thing. 😂 Anyway, really appreciate ur point.
@heathens2867
@heathens2867 2 жыл бұрын
@@tahaansari5621 it's actually good because not everyone will watch his each and every video
@최강재-y9c
@최강재-y9c 2 жыл бұрын
It is very informative comment since choosing what data structure will be used is the most important one when solving the tests. Thanks a bunch
@tomerharari633
@tomerharari633 2 жыл бұрын
What's wrong with a hashmap though? because checking if hashmap contains a key is also O(1) isn't it?
@dimka781
@dimka781 Жыл бұрын
​@@tomerharari633 it's the same. check how HashSet is implemented internally, it's backed by HashMap. the only difference with add and put(key, value), but you can do Contains in both cases, for map it's containsKey, containsValue, when for Set it's contains. it's eventually your choice.
@MrMakemusicmike
@MrMakemusicmike 3 жыл бұрын
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.
@utkarshpant5297
@utkarshpant5297 4 жыл бұрын
Thank you man... I was smashing my head over this problem for more than 3 hours
@jibinkjose1073
@jibinkjose1073 3 жыл бұрын
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)
@ashokred
@ashokred 2 жыл бұрын
Ditto, this will fail for "aabaab!bb" unless this fix is applied :)
@kunalnarang1912
@kunalnarang1912 2 жыл бұрын
This will still work because a_pointer was never incremented if duplicate was found. So this will always give you the correct answer. I'll recommend working this through a example string.
@raghavendrayadalam7440
@raghavendrayadalam7440 2 жыл бұрын
I'm actually trying with map. can you please share code?
@punters4218
@punters4218 Жыл бұрын
@@kunalnarang1912 I had to tweak this solution a bit to make it work as it didn't seem to work for string "pwwkew" class Solution { public int lengthOfLongestSubstring(String s) { int maxLength = 0; int aPointer = 0; int bPointer = 0; Set visited = new HashSet(); while(bPointer < s.length()){ if(!visited.contains(s.charAt(bPointer))){ visited.add(s.charAt(bPointer)); bPointer++; maxLength = Math.max(maxLength, visited.size()); } else { while(s.charAt(aPointer) != s.charAt(bPointer)){ visited.remove(s.charAt(aPointer)); aPointer++; } visited.remove(s.charAt(aPointer)); aPointer++; } } return maxLength; } }
@dimka781
@dimka781 Жыл бұрын
@@ashokred it won't fail.
@btsforever7815
@btsforever7815 2 жыл бұрын
you are single-handedly carrying my leetcode journey, thank you so much!
@ryzurrin
@ryzurrin 3 жыл бұрын
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!
@albertgao7256
@albertgao7256 3 жыл бұрын
This code is obviously much readable than the official LeetCode solution, great job dude!
@mattgervasio3798
@mattgervasio3798 4 жыл бұрын
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.
@amitverma0511
@amitverma0511 4 жыл бұрын
It will fail, if we have input like abcabcdbbef. It you won't remove char, then output will be 6.
@rushm6684
@rushm6684 2 жыл бұрын
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.
@TheSmashten
@TheSmashten 2 жыл бұрын
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
@manavmody4959
@manavmody4959 2 жыл бұрын
the max stores the maximum of hashset size and current max..also note we have to check each and every substring.
@M240Waheed
@M240Waheed 5 жыл бұрын
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
@NickWhite
@NickWhite 5 жыл бұрын
Abdul Ahmed number 1 tip - don’t sit there and try and solve it. Just look at the solutions and learn
@M240Waheed
@M240Waheed 5 жыл бұрын
@@NickWhite I'll try that out thanks for the reply
@justskillfull
@justskillfull 4 жыл бұрын
...also after you've completed a few, you'll have a basic understanding that most of the questions following the same principles which allow you to solve similar questions with the same or slightly different solutions.
@fake_tourist
@fake_tourist 3 жыл бұрын
@@NickWhite what if I keep looking at solutions then? I sometimes feel that if I have to lookup solution, it is kinda cheating and not the natural process of learning.
@trailsnail
@trailsnail 3 жыл бұрын
@@fake_tourist think of it like cheating but to alter the natural process of learning in a positive direction.
@adrijaroy4499
@adrijaroy4499 2 жыл бұрын
Awesome explanation man!! Just that you missed two test cases: if(s.isEmpty()){return 0;} if(s.length()==1){return 1;}
@bethlehemyohannes200
@bethlehemyohannes200 2 жыл бұрын
He didn't miss those cases. His solution returns 0 for empty string and 1 when the string length is 1
@TheKillermob13
@TheKillermob13 3 жыл бұрын
bug : try this input " abcabkabc" when b_pointer at 7 , it will remove a_pointer at 3 . the set will have "abka"
@ameynaik2743
@ameynaik2743 3 жыл бұрын
It works - b_pointer doesn't increment. Try to step through in a debugger.
@dalbirsinghdhillon1429
@dalbirsinghdhillon1429 3 жыл бұрын
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.
@TheDoubleMvp
@TheDoubleMvp 3 жыл бұрын
The remaining string will be "aca", that's right. Keep going through the while-loop though. On the next iteration of the while loop, b_pointer will still be at the second "a". Since "a" is still in the hash_set, the code will go to the else block, remove "a" from the hash_set, and increment a_pointer. Then, you're left with "ca". Notice that b_pointer is still at "a". So in the next iteration, it will add "a" to the hash_set again since we're still at that char and we've just removed the other "a".
@SK-lu7ci
@SK-lu7ci 4 жыл бұрын
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..??
@ianpan0102
@ianpan0102 4 жыл бұрын
I see your point -- the fact is Nick didn't explain this part clear enough. Beware of the fact that b_pointer will not move unless the duplicate is cleared. So you can see it as a continuous loop until the duplicate at b_pointer is found and removed from the substring, then the b_pointer will go on incrementing. Otherwise, a_pointer is just going to continue moving right while b_pointer sits still, letting the substring shrink every iteration.
@SK-lu7ci
@SK-lu7ci 4 жыл бұрын
@@ianpan0102 Make sense . Thanks for explaining. Until whatever duplicate is removed only "else part" is executed shrinking the hash . Finally after duplicate is removed, b_pointer will progress again.
@ianpan0102
@ianpan0102 4 жыл бұрын
@@SK-lu7ci You're welcome
@AtulDislay
@AtulDislay 4 жыл бұрын
@@ianpan0102 thanks I had the same question after I went through the solution
@jairambala4276
@jairambala4276 4 жыл бұрын
He didn't explain but the code does remove until that duplicate is removed.
@julietgeorge4858
@julietgeorge4858 4 жыл бұрын
I love you!!! I may not pass my interview but the knowledge you are here giving away for free is amazing!
@mohdamirkhan6571
@mohdamirkhan6571 3 жыл бұрын
Thanks. I love you too
@mikasaackerman2694
@mikasaackerman2694 4 жыл бұрын
Thanks! It's one of the simplest codes of this question I've seen so far.
@steeck1
@steeck1 3 жыл бұрын
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. :)
@gradientO
@gradientO Жыл бұрын
Same. Nick is legit
@주모미디어
@주모미디어 4 жыл бұрын
u keep sating it is easy but it wasnt easy until you showed me.
@Minte123
@Minte123 3 жыл бұрын
XDDD
@fatimaibrahim-biangoro9607
@fatimaibrahim-biangoro9607 4 жыл бұрын
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?
@farhan787
@farhan787 4 жыл бұрын
Right
@niiazemiluulu9987
@niiazemiluulu9987 4 жыл бұрын
W8, but there are “while” loop and inside “contains” method that has O(n) complexity. So, at the end we solution with O(n^2) time completely
@rohitkasture0100
@rohitkasture0100 4 жыл бұрын
@@niiazemiluulu9987 Hashset contains method has time complexity O(1). so time complexity will be O(n)*O(1)=O(n).
@sanjayizardar2263
@sanjayizardar2263 4 жыл бұрын
Algorithm of the day. Awesome job buddy... Keep up the great work.
@Kidkromechan
@Kidkromechan 2 жыл бұрын
Nice. Thank you. You can also add a while loop on your else statement to make it a little faster. while (hash_set.contains(s.charAt(b_pointer))) { hash_set.remove(s.charAt(a_pointer)); a_pointer++; }
@giovankabisano2266
@giovankabisano2266 Жыл бұрын
Real curious. Why adding a while loop making it faster? Does it add more complexity to the code?
@yeojinha930
@yeojinha930 4 жыл бұрын
Thanks man! I tired to solve this problem over 1 hour... I thought I made a right solution and submitted , Time Limit Exceeded baaaam!!!!!!!!
@chathrapathis1110
@chathrapathis1110 2 жыл бұрын
I searched for a logic almost half a day. You have helped to pick the logic to solve this problem. Thank you so much :))))
@Blure
@Blure 4 жыл бұрын
Thanks for the explanation, this problem took me a while to understand lol.
@bossman4112
@bossman4112 3 жыл бұрын
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?
@Ben-pb7ct
@Ben-pb7ct 3 жыл бұрын
This is a very straightfoward solution and approach to the problems. Could anyone verify with me the time complexity is O(N) and space complexity is O(N) because of the size of HashSet?
@jainayak666
@jainayak666 3 жыл бұрын
This was excellent! Just one question, wouldn't you want to remove the charAt(b_pointer) from the HashSet instead of a_pointer? For instance, for a string abcc, when you see the second 'c', you'd actually be removing 'a', correct?
@Chris-xr2bt
@Chris-xr2bt 3 жыл бұрын
He doesn't explain it, and there are other videos that do, but since it removes a and doesn't increment the b pointer it would check again and then remove b, and then c until only that 2nd c remains and the substring checks continue. The idea is that the window grows to the right (bpointer) as long as characters are unseen and shrinks from the left (apointer) until the duplicate that triggered the shrink gets removed. Leaving you to find the longest substring based on the size of the window.
@codingpros4391
@codingpros4391 4 жыл бұрын
Hey thanks for the explanation. Could you tell me how does it work for this test case: "pbbbyylmkbn"
@tddod5060
@tddod5060 4 жыл бұрын
I have the same question. Basically, if the repeating character is not the first element in the subset. I think the algorithm cannot handle.
@albertli7044
@albertli7044 4 жыл бұрын
Take "pww" for example. If the b_pointer encounters w, at index 2, we will remove "p" from the set. At this time, the set is [w], and b_pointer still points to the "w" at index 2. After removing "p", since the set still contains the value of b_pointer, we will again removing "w" from the set. Now the set becomes []. The two pointers both point to the index of 2, and we add the w at index 2 to the set.
@mehulgala07
@mehulgala07 4 жыл бұрын
​@@tddod5060 Spot on. It's a wrong algo. I'm wondering how it worked at his end?
@andychang1179
@andychang1179 4 жыл бұрын
Me wondering too
@faaith100
@faaith100 4 жыл бұрын
I think this can be fixed by using an array instead of a hashmap. The big problem is the complexity would go up big time
@raxit2684
@raxit2684 4 жыл бұрын
Just curious, why didn't you try to improve your runtime on this one?
@pealhasan
@pealhasan 9 ай бұрын
Probably the easiest explaination :) This really helped me understand the sliding window problems.
@misokwon4713
@misokwon4713 5 жыл бұрын
very clear and concise! thank you!! it was super helpful : )
@raghulkrishnanb8919
@raghulkrishnanb8919 3 жыл бұрын
Say the given string is "abbb" According to the code in the video - a_pointer and b_pointer = 0; first - 'a' gets into the hashset, next - 'b' gets in. So far OK. (a_pointer is still 0 and b_pointer goes to 2) Then, the second 'b' arrives - now 'b' already exists in the set, so it goes to the else part and removes s.chartAt(a_pointer) which is 'a'???? It should be removing 'b'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Though the max variable helps get the answer, is the procedure right?
@qIukas
@qIukas 3 жыл бұрын
This is completely wrong. Advances the left index by 1, assuming that the repeated character is always there, but it might be anywhere in the substring.
@umarjaved56
@umarjaved56 3 жыл бұрын
I'm thinking the same thing
@Chris-xr2bt
@Chris-xr2bt 3 жыл бұрын
It isn't explained, but since the right index isn't incremented the loop checks the set again for the duplicate. The idea is that it shrinks the window from the left until the duplicate is removed and the current letter that triggered the duplicate found condition can be inserted into the set. This gives it a similar operation to using a map to index each letter and then shrink the window to the index following a duplicates last seen index. The difference being with a map you can immediately shrink the window to the new size rather than decrement the window until you find the new size.
@ranaalameedee9762
@ranaalameedee9762 2 жыл бұрын
You are awesome, I trying to understand this for hours, and you just make it easy in 8 minutes. Thank you.
@MrDheeraj14
@MrDheeraj14 4 жыл бұрын
Solution is good but what if we replace string S="abcabcbb" to S = "abcbacbb" then the above solution will remove 'a' instead of 'b' because of a_pointer. string s = "abcbacbb"; if (s.Length == 0) return; int result = 0; int[] cache = new int[256]; for (int i = 0, j = 0; i < s.Length; i++) { j = (cache[s[i]] > 0) ? Math.Max(j, cache[s[i]]) : j; cache[s[i]] = i + 1; result = Math.Max(result, i - j + 1); }
@pravinpoudel1307
@pravinpoudel1307 2 жыл бұрын
Exactly !!! Thats what i was thinking
@mrbanana6651
@mrbanana6651 2 жыл бұрын
I was thinking the same but then I saw "b_pointer is only increased while adding to hash set" otherwise "it will keep going to else statement and carry on deleting and increasing a_pointer". Also to maintain insertion order LinkedHashSet is used which is missing.🙂
@mathssr
@mathssr Жыл бұрын
I naturally gravitated towards the same solution, but I think left pointer and right pointer runs over the same element twice which makes this solution not the most optimized one. It is still an O(2n) solution which is not bad but it could be better.
@moonmaster36
@moonmaster36 2 жыл бұрын
Much easier to understand than the explanation in Grokking the Coding Interview.
@Moosorkh
@Moosorkh 2 жыл бұрын
Thank you for the video, I wish that you ran the code to give it a test and also see that there a miniscule typo in it. But, that does not change the fact that the solution is straightforward.
@qileliang7044
@qileliang7044 4 жыл бұрын
I think there is something wrong with your solution. If there is a string "pwwkew", when your left pointer points to the "p" and the right pointer point to the second w, the HashSet will remove "w". However, at this time, your left pointer is pointing the first " w " and the right pointer is pointing the second" w ", and the letter "w" is not in the HashSet.
@senaokumus9548
@senaokumus9548 Жыл бұрын
Guys, I understand why we remove character in else clause but why we remove character in a_pointer. I was expecting to remove b_pointer since we check if character in b_position is present in Hashset? What if string is dbcabcbb? when you found b is already in Hashset you'll have to remove d at the beginning. We should've remove b, right?
@nikhil-saxena
@nikhil-saxena 4 жыл бұрын
Very nicely done. But I think there is one issue with the substring content.
@allthingsuseless4707
@allthingsuseless4707 2 жыл бұрын
Explained well. But the string we are getting at the end in hashset is not the expected string always. As we are returning the max size of the substring which is still intact because of the the Math() (max) operation.
@sajalsingh7768
@sajalsingh7768 3 жыл бұрын
Everytime I start practising for interviews I get stuck on this question but Nick White always helps
@nishantingle1438
@nishantingle1438 2 жыл бұрын
else block needed more explanation though dry run gives the logic
@yvoonetao4288
@yvoonetao4288 3 жыл бұрын
I watched few videos for this question. this solution is so genius!!! Thank you for your video!
@saderied8719
@saderied8719 10 ай бұрын
When we get to a character that is already in the set, how do we know that the last time we seen that character it was at the index of a_pointer? I am a bit confused as to how we know that always removing the character at a_pointer from the set will always be the duplicate character? Like how do we know the duplicate character is not any other character between a_pointer and b_pointer?
@jpenber5418
@jpenber5418 3 жыл бұрын
Useful for explaining the "sliding window" solution, ty!
@johnangelo2000
@johnangelo2000 3 жыл бұрын
Why remove @a_pointer? Is it because we have to find the last occurence of current char in hash to deduce length? Also we can do this max = ((b_pointer- a_pointer)>max)? (b_pointer - a_pointer):max; for max isn't it?
@brianspinos
@brianspinos 4 жыл бұрын
You could move the a_pointer to the index after the first repeating character, instead of traversing the sequence, that would skip a lot of iterations!
@Fr0zen_
@Fr0zen_ 4 жыл бұрын
What if the input was "abcade"?
@brianspinos
@brianspinos 4 жыл бұрын
FrZn it would still find “bcade” :)
@brianspinos
@brianspinos 4 жыл бұрын
FrZn the a_pointer would move from a to b
@brianspinos
@brianspinos 4 жыл бұрын
FrZn here is a better input to illustrate that: abcDefgDhijklmnop
@brianspinos
@brianspinos 4 жыл бұрын
FrZn first iteration would start in a, second in e, skipping 3 iterations
@meriyemelhajoui4083
@meriyemelhajoui4083 11 ай бұрын
in the example of pwwkew first iteration will be : storing p and then storing w and then removing w so I increment , so now j will be indexed to W it will add it to the set , now we have w k e but then we have w so we gonna remove it from the set , finally we gonna get only ke which is not correct
@RPMforever4
@RPMforever4 17 күн бұрын
instead of contains method, if we use directly add , then we can reduce one line if(set.add(s.charAt(rightPointer))){
@90krishika
@90krishika 4 жыл бұрын
Excellent explanation.. Liked it. You don't have to worry about the errors, it happens with everyone. You are making us understand and explaining the alogorithm so well - that's the most considerable and fabulous thing.
@iubob98
@iubob98 4 жыл бұрын
could you draw the solution for better visualization? like where the pointers are currently at now etc. ?
@Shiva-zy7jq
@Shiva-zy7jq 4 жыл бұрын
Yea. it will be helpful if we can draw and explain
@prachisharma7500
@prachisharma7500 Жыл бұрын
Hey Nick kudos to you great work ! i would appreciate if in evry video you can also explain time and space complexity of the sol
@MAGAVISHNUT
@MAGAVISHNUT 6 ай бұрын
What if there is an empty strings is present in s variables will the code executes???
@blancosj
@blancosj 4 жыл бұрын
this solution applies sliding window strategy. It can be solved recursive, so memoization too.
@saipeor
@saipeor 2 жыл бұрын
Doesn't work for "pwwkew", solution doesn't ensure that the hash set contains a substring. It will keep 'p' in the hashset unti the next p, but if there isn't one, your final max will be 4 as you can have a hashset p,w,k,e when it should hbe w,k,e (or k,e,w). Since youtube deleted the dislike count, now a ton of newbies will think this is the correct solution and fail their interviews.
@diamondlee478
@diamondlee478 2 жыл бұрын
I just ran it with this example and it absolutely does work.
@saipeor
@saipeor 2 жыл бұрын
@@diamondlee478 strange , look at comments . Other people have the same issue but also just look as the logic I wrote, does it not make sense?
@hey-cg3fv
@hey-cg3fv 2 жыл бұрын
Bro , solution is correct.when 'p' comes it adds it in set incrementing "b"(b at 1),when next character 'w' comes it also added in set ,incrementing the "b"(b at 2) ,max value =2,now when the second 'w' comes it removes the character at "a"(0) position which is 'p' incrementing "a"(a at 1) and set size is now=1 and when we iterate again at b index it is still 'w' so remove 'w' from set now the set size is zero,when we iterate again as 'w' is not in set it added in set incrementing b. likewise the code runs.
@saipeor
@saipeor 2 жыл бұрын
@@hey-cg3fv look all I know is I run this on leetcode and that’s the input that failed, try it yourself
@hey-cg3fv
@hey-cg3fv 2 жыл бұрын
@@saipeor I run this code yesterday it executes perfectly
@shenzheng2116
@shenzheng2116 4 жыл бұрын
How to write that Java code in Python? I have searched many webs but have found no concise Python Solution. Thanks very much in advance.
@teshou4474
@teshou4474 4 жыл бұрын
There are different tricks in Python that aren’t exactly similar to Java code, what you can do is look at this exact question on leetcode then look in the discussion comments and look at the python solutions people have come up with. That should help
@Kai-iq2ps
@Kai-iq2ps 3 жыл бұрын
Eh, Idk, maybe just write it in python following the same logic line by line? Following works, everybody. def lengthOfLongestSubstring(self, s: str) -> int: if (len(s) == 0): return 0 left = 0 right = 0 ls = list(s) longest_set = set() longest = 0 while right < len(ls): if ls[right] in longest_set: # If duplicate seen, two type of "duplicate seen" longest_set.remove(ls[left]) # very important, remove the left pointer element left += 1 else: # ls[i] not in longest_map, aka see a new character non-duplicate longest_set.add(ls[right]) longest = max(longest, len(longest_set)) right += 1 return longest
@fufuto
@fufuto 2 жыл бұрын
Why can't we solve it with only one pointer and a set? One pointer iterates through the array meanwhile adds the chars into a unique set. Then whenever we see the same char (if it can not be added into unique set) we get the max length and clean the set. Then continue until the end of the array.
@chriswajega2430
@chriswajega2430 2 жыл бұрын
public static int lengthOfSubString(String input){ List subString = new ArrayList(); Arrays.asList(input.split("")).forEach(val -> { if (!subString.contains(val)) subString.add(val); }); return subString.size(); }
@rishabhsahu5257
@rishabhsahu5257 4 жыл бұрын
Thanks Nick! You made problem so easily explained !!You are genius Man.
@narasimhakamath3808
@narasimhakamath3808 2 жыл бұрын
The easiest explanation out there. Thank you very much.
@hickton45
@hickton45 4 жыл бұрын
I have a question about LeetCode in general... Are people expected to derive these out by themselves, coming up with the sliding door idea from nothing, or do most people learn how to implement it through a video like this?
@NickWhite
@NickWhite 4 жыл бұрын
people know from doing them
@ruchigupta23
@ruchigupta23 4 жыл бұрын
@@NickWhite HashSet contains unique values then what's the need to remove characters from it?
@ruchigupta23
@ruchigupta23 4 жыл бұрын
@@NickWhite also what's the need for max hashset size will return the same value?
@МаксимЕремин-я6т
@МаксимЕремин-я6т 3 жыл бұрын
Thank u. Spend the hole day trying to understand it.
@shubhankitsaxena2650
@shubhankitsaxena2650 3 жыл бұрын
Wrong Answer: Input: "pwwkew" Output: 4 Expected: 3
@coding_withai
@coding_withai 2 жыл бұрын
Yes there are more things to do in else block for it it work. I was doing it in PHP so slightly different but in the else block I had to cut out the string from the found letter until the end plus add the found letter to the end of the string and then it worked. The basics of the examples and the way of thinking are good though in the video.
@eugenek951
@eugenek951 Жыл бұрын
I enjoyed this explanation. Thanks so much!
@RA-nt1hj
@RA-nt1hj 3 жыл бұрын
Hi! Can you please explain this same program by using the ascii code method?
@abdullahbabor4876
@abdullahbabor4876 3 жыл бұрын
Incredibly nice explanation! Hats off to you!
@sidikridwan
@sidikridwan Жыл бұрын
how about "abbcc" since you a_pointer index is zero and the duplicate character is in index 1 ?
@cryptojeff3993
@cryptojeff3993 3 жыл бұрын
This is Gold! Thanks! So clear! Thanks Nick!
@uk1523
@uk1523 4 жыл бұрын
Can you implement in cpp?
@theAppleWizz
@theAppleWizz 4 жыл бұрын
This solution does not work with PWWKEW
@jeezradz
@jeezradz 4 жыл бұрын
when you delete the key in the hashset -- aren't you supposed to add it back for the same char but at a newer index?
@jeezradz
@jeezradz 4 жыл бұрын
sorry never mind... I didn't realize we are not incrementing the end in the else part
@sarahy5991
@sarahy5991 2 жыл бұрын
Yes! Great video. I shall continue on my journey - no nervous breakdown tonight. lol. Thanks man :)
@dineshmaddisetty2545
@dineshmaddisetty2545 3 жыл бұрын
Does this work for the input string as "dvdf". leet output return is 3.
@SK-yb7bx
@SK-yb7bx 2 жыл бұрын
Great job, Nick. It's well explained.
@williengangamacharia2761
@williengangamacharia2761 2 жыл бұрын
Thanks man for this straightforward answer!
@nguyentuan1990
@nguyentuan1990 2 жыл бұрын
thanks for explanation, was stuck at this problem for a long time
@imykeX
@imykeX 3 жыл бұрын
Thanks a lot Nick. This is really nice and concise... O(n) time and won't take too much space...
@kks2105
@kks2105 5 ай бұрын
Is this finding the sub-string or the sub-sequence, I think the solution above fails for few cases. Has anybody noticed ?
@lucasslf
@lucasslf 3 жыл бұрын
After finding a repeated char, shouldn't a_pointer be set to the next index after the first index of the repeated char?
@iUwej-y8d
@iUwej-y8d 3 жыл бұрын
Since we are not incrementing b_pointer in case of a repeated char, the next couple of iterations will use the same repeated char and move the a_pointer to the next index after the first index of the repeated char.
@lucasslf
@lucasslf 3 жыл бұрын
@@iUwej-y8d thanks!
@amansingh.h716
@amansingh.h716 3 жыл бұрын
thanks nick this explanation is great mylogic and this logic 100'% sync i regret why I didn't come on this video at first place
@Codewithwaseem
@Codewithwaseem 3 жыл бұрын
This does not work for string "Clementisacap ", mentisac answer should be 8 , i get 10 as max
@vcfirefox
@vcfirefox 3 жыл бұрын
So why are you removing "a" pointer when hashset contains element pointed by "b" pointer? Meaning, if i deal with "abcbc" when the end is at second"b" your algorithm removes a just wanted to understand why
@scotty8789
@scotty8789 3 жыл бұрын
What I think happens, in the case of "abcbc" where the duplicate character is not the first character, each loop, an element will be removed until the duplicate character is removed. So yes the "a" will be removed when we have the second "b" but then on the next loop, the first "b" will be removed as well so then our hash set will just be "c" at which point it will try to add the second "b" again and will be successful since the first "b" was removed.
@hey-cg3fv
@hey-cg3fv 2 жыл бұрын
to decrease the size of set
@rushabhjaiswal1442
@rushabhjaiswal1442 5 жыл бұрын
qrsvbspk Can you explain for this input please ? what is the output and how?
@MoaazAhmad
@MoaazAhmad 5 жыл бұрын
It's 5: "qrsvb" is the longest substring. If you add the 's' after 'b', you get a repeating character. All other substrings without repeating chars are shorter than this one.
@anuabraham2524
@anuabraham2524 5 жыл бұрын
@@MoaazAhmad vbspk also mate ✌️
@angelbythewings
@angelbythewings 3 жыл бұрын
Man that's a sweet solution to the problem
@darwinanirudh1793
@darwinanirudh1793 4 жыл бұрын
Can some one explain the me reasoning behind removing the header of the set when we encounter a existing character .
@zacharysong4863
@zacharysong4863 3 жыл бұрын
It also confuse me, I think remove the head is wrong.
@IChowdhury01
@IChowdhury01 3 жыл бұрын
I don't get it either bro, rip
@henryborska9098
@henryborska9098 3 жыл бұрын
Thanks man. This solution is way cleaner
@Prince-fv8mb
@Prince-fv8mb 8 ай бұрын
why do we use the hashset.remove function in else part
@aartikelkar397
@aartikelkar397 4 жыл бұрын
Simplest way.. Very clear thanks
@eshagupta9407
@eshagupta9407 4 жыл бұрын
Beautiful explanation man!
@kuttikrishnankodoth1463
@kuttikrishnankodoth1463 2 жыл бұрын
Thank you , you are doing great man .. this videos are very helpful ..!
@madanmohanpachouly6135
@madanmohanpachouly6135 2 жыл бұрын
Nice explanation, Thanks.
@Asingh42
@Asingh42 21 күн бұрын
Instead of the set I used a 128 size fixed array boolean to get more performance!
@angkits9709
@angkits9709 4 жыл бұрын
How can we calculate the time complexity of the code ?
@whiskeydonald4907
@whiskeydonald4907 3 жыл бұрын
I thought the same idea.Only thing that made my solution O(N^2) was that i didn't know that hashset remove has a time complexity of O(1) until you brought it up. How can i be this stupid :P
@Bargains20xx
@Bargains20xx 3 жыл бұрын
This can be improved a little bit by adding a hashmap and storing locations of each index encountered or by keeping the map in an array depending on the collation
@nguyentuan1990
@nguyentuan1990 2 жыл бұрын
can you elaborate this part? storing the locations of each index?
@damodharreddypannala3604
@damodharreddypannala3604 2 жыл бұрын
well explained thank you so much Nick
@ArifRahman-nb3ff
@ArifRahman-nb3ff 2 жыл бұрын
you are the best Nick :) Thank you!
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 8 МЛН
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 14 МЛН
REAL 3D brush can draw grass Life Hack #shorts #lifehacks
00:42
MrMaximus
Рет қаралды 8 МЛН
LeetCode 14. Longest Common Prefix Solution Explained - Java
6:33
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1 МЛН
Longest Repeating Character Replacement - Leetcode 424 - Python
20:44
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Leetcode 3. Longest Substring Without Repeating Characters
15:49
Code with Alisha
Рет қаралды 70 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 661 М.
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 8 МЛН