LeetCode Exercise In Java - Longest Substring Without Repeating Characters - FAST Solution

  Рет қаралды 83,386

Coding with John

Coding with John

Күн бұрын

Full tutorial for solving the LeetCode longest substring without repeating characters problem in Java!
Thanks to Mailgun for sponsoring this video! Go to mailgun.com/john to try Mailgun today.
00:00 The Problem
03:51 Brute Force Solution Discussion
07:27 Brute Force Solution Implementation
15:57 Brute Force Solution Results
16:35 Coming Up With a Better Solution
20:09 Fast Solution Discussion
26:50 Fast Solution Implementation
32:34 Fast Solution Results
33:21 Even... FASTER?
☕Complete Java course:
codingwithjohn.thinkific.com/...
Are you starting to grind LeetCode in Java, but getting stuck on the how to figure out some of the trickier fast solutions?
In this video we'll walk through a fast solution for the LeetCode longest substring without repeating characters problem, LeetCode Problem #3, written in Java!
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Camera I use for recording:
amzn.to/3wlXcmR
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com

Пікірлер: 175
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Thanks to Mailgun for sponsoring this video! Head to mailgun.com/john to try Mailgun free today. Looking forward to seeing how you guys make this solution even better!
@aryansanojraj6618
@aryansanojraj6618 Жыл бұрын
Can you let us know the compiler you use for the videos?
@ripanpramanick425
@ripanpramanick425 Жыл бұрын
Not able to understand how this algo will work for some string like "abcdcga" as we will keep our left pointer at first a and we will increment right pointer, at when right pointer at second c it will get repeated letter and will increment left pointer to b and in next iteration it will move to g and will register length of bcdcg but this substring is already having repeated character. Rather than incrementing left pointer by one we should increment it by lastCharOccuredIndex+1
@playtopgames3261
@playtopgames3261 Жыл бұрын
Best java chanel on KZbin
@ernestchikuwa9935
@ernestchikuwa9935 Жыл бұрын
Excellent!!
@nahomg6945
@nahomg6945 Жыл бұрын
You genuinely have one of the best programming explanation videos on this site, honestly. Funny, I was discussing learning about the Sliding Window algorithm to practice Leetcode questions with a friend yesterday, and lo and behold you've uploaded a great explanation literally 24hrs later, legendary! Could you please upload more Leetcode explanation questions? If not on KZbin, perhaps a course?
@Achraf_0101
@Achraf_0101 Ай бұрын
i already signed up in his course but it does not contain any exercises i wonder if there is another course where he solves problems
@vijal-patel
@vijal-patel Жыл бұрын
please keep doing these, your explanations are so much more in depth than other youtube channels
@jayshreebarathod6997
@jayshreebarathod6997 Жыл бұрын
I had just solved this problem and found this video. You explained each solution and your approach in the best way. Your way to explaining make things crystal clear John !!
@claytonalmeida6046
@claytonalmeida6046 Жыл бұрын
Would love to watch a complete java dsa course from you
@jayshreebarathod6997
@jayshreebarathod6997 Жыл бұрын
DSA would be more easy and interesting with John 😀
@thomas_m3092
@thomas_m3092 Жыл бұрын
John, you are a wizard. Everythings looks so logical and simple. Please make more videos like this.
@tund_101_hd9
@tund_101_hd9 Жыл бұрын
This is probably one of the best tutorials out there and i think KZbin is with me in this.❤️ I could you not, even though I have watched it already it is every time the first video in my recommended 😂 Guess I'll watch it twice. Keep up the good work!
@isaacwhiz
@isaacwhiz Жыл бұрын
You are really a coding geek. Even the concepts known, still visit them and keep something. Your explanations are far reaching. Thank you.
@minakianrad812
@minakianrad812 Жыл бұрын
An excellent in-depth explanation of two approaches to solving this problem. Thank you very much, John.
@yasasmaddumage
@yasasmaddumage Жыл бұрын
I love this series. Thank you...
@Michaelpschreib
@Michaelpschreib 4 ай бұрын
I appreciate that you go into the brute force solution and also the more clever solution. Thanks for these!!
@zerkinanastya4410
@zerkinanastya4410 10 ай бұрын
Definitely one of the best explanations or just the best, starting from the basic one (brute force) just to kick off and let viewers like me grasp the idea before jumping to more complex solutions. Thank you!
@SimanAndGarfunkel
@SimanAndGarfunkel 3 ай бұрын
I love the final thoughts on the differences of performance between the 2 last algorithms, answered all the questions I had to myself. Thnak you!
@ArunKumar-jk5pq
@ArunKumar-jk5pq Жыл бұрын
This is awesome way of teaching! Thanks John!
@lanatimmo3686
@lanatimmo3686 Жыл бұрын
It's great! Please, do more Leetcode Exercise explanations!🙏
@Tibetan-experience
@Tibetan-experience Жыл бұрын
Thanks John. Always putting these awesome videos.
@ronakpatel8441
@ronakpatel8441 Жыл бұрын
Hey John, I love all of your videos. Learning so many important skills. However, I have one suggestion, If you could make more of this type of problem-solving videos then it would be very helpful to the viewers as it will teach new programmers how to think of a solution to the given problem and how can we actually implement the solution using the programming language. By the way thanks again for all efforts that you put in to make this possible. Looking forward to seeing more problem-solving videos. Have a wonderful day.
@rushio8673
@rushio8673 Жыл бұрын
Great job, please keep posting the leetcode solution videos in structural manner(for eg top 50 that includes most practices/datastructures) , this helps a lot in preparing for interviews.
@Soromeister
@Soromeister Жыл бұрын
Love the series and quite a nice explanation on the different approaches, although the last one is confusing for me.
@zainahmed755
@zainahmed755 Жыл бұрын
Best channel ever ! , please keep going with the leatcode series
@kafychannel
@kafychannel Жыл бұрын
Great explanation,thank you !
@tiagocarvalho4119
@tiagocarvalho4119 Жыл бұрын
You have to be careful when using a MAP to lookup something because, even though its O(1) in time complexity, there is a lot of overhead. Let's say that the constant time for a lookup in a MAP is always 500 nanoseconds, if the indexOf (that has a time complexity of O(n)) takes only 300 nanoseconds because it finds the answer in the first few characters, then the indexOf will perform better. This obviously can be measured and we can come up with some threshold that tells us what algorithm to use in each situation. My guess is that in the English language indexOf will always perform better because we are dealing with words that are small in size (there is always the repeating space character).
@Phl3xable
@Phl3xable 11 ай бұрын
My thoughts exactly
@olyamychko4315
@olyamychko4315 Жыл бұрын
Thanks for great explanations!
@kevolala
@kevolala 6 ай бұрын
This is the most detailed and accurate explanation of this problem I've seen. 👍
@liamoua9192
@liamoua9192 Жыл бұрын
John, you’ve helped me understand DS&A in Java better than before. The way you explained things are easy to understand and follow. I’m a visual learner and your explanations makes it easier for me visualize & understand. Thank you and please keep these leet code videos coming. Your other videos on Java has truly helped me understand Java so much more than my professors lol you are incredible in the work that you’re doing for me and many. We cannot thank you enough ❤ I appreciate you.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Thanks so much for the very kind words!
@91-ritikjain36
@91-ritikjain36 7 ай бұрын
when we get full playlist of leetcode in java you are amazing i am from india @@CodingWithJohn
@blairliu9058
@blairliu9058 Жыл бұрын
More leetcode videos plzzz! love this series.
@CostaKazistov
@CostaKazistov Жыл бұрын
💯
@hikefka8001
@hikefka8001 Жыл бұрын
Yes please, solve all the challenges :D
@sankalp.m.patilsmp1472
@sankalp.m.patilsmp1472 Жыл бұрын
this channel has wonderful content, and easy to understand explaination.
@markmuyot2436
@markmuyot2436 2 ай бұрын
I liked your videos, you made them all easy to grasp! please do more leetcodes tutorials and data structures and algorithms content
@zoflax
@zoflax Жыл бұрын
this series is actually so good.
@foratzoabi1311
@foratzoabi1311 4 ай бұрын
Hey John! First of all thanks for the amazing video! Short answer for why indexOf is faster in this case: It is simply because in this case it has an O(1) runtime, how? here is how: Before you slide your right pointer to the right you have already made sure that this substring has different characters, which in the worst case will be 24 characters long. Since you know it will never be longer than 24 characters, or 34 if numbers are included in the string, then it is constant time, because no matter how big your string will be, it will be in the worst case that maximum substring :D so it is related to the valid substring and not to the input! :D And since the map indeed has access and write time of O(1) it still has an overhead for hash calculation and storing and etc... which take more operations / time than indexOf in that case, but since none of them is related to n, the one with the shorter/faster operations wins :D I hope that was helpful!
@chrisjames278
@chrisjames278 Жыл бұрын
great video. Keep them coming!
@MTB_Bay_Area
@MTB_Bay_Area Жыл бұрын
For the brute force solution. I suggest checking if the max need to be updated in the "if contains part" that makes sure that we only do it right before we break. It is one time per substring. We fount a substring, now, let's check if it is better than the other we had so far. Second, I will suggest using HashSet instead of StringBuilder. HashSet has contains method and it is faster.
@OsaetinEvbuoma
@OsaetinEvbuoma Жыл бұрын
Hi John. I previously implemented this solution (yours) using a while-loop (and using the map) instead of a for-loop and got similar runtime numbers to the last solution you found (6ms runtime). Not sure why that's the case. But yeah, the last solution, intuitively should be a worse runtime even though it seems like it performs better. It's also a clever solution too. Nice work!
@elizabeth00653
@elizabeth00653 7 ай бұрын
John I love your Java explanations, can you please add more leetcode to your channel or bootcamp? I would love to see more Java leetcode solution guides properly explained
@EdgarRamirez-ry2je
@EdgarRamirez-ry2je Жыл бұрын
Excelent explication of a leetcode coding challenge, thanks i hope to see more videos of coding challenges medium and hard
@pedroalves5482
@pedroalves5482 Жыл бұрын
Hi, first time watching your videos, and I love the clarity of your explanation. I think I figured out the reason of Map been more slowest than last example, when you use the hash map you as writing and updating the data, to have the current letter position. On the other side the s.index only search if exist this value.
@SaifaldeenSAH
@SaifaldeenSAH Жыл бұрын
The best explaining ever, hoping to make more videos plz or DS and Algo videos. Thank you.
@user-kuzya2023
@user-kuzya2023 Жыл бұрын
Спасибо Джон. Продолжайте и дальше радовать нас своими видео. Thanks John. Continue to delight us with your videos
@shaileshsathe9779
@shaileshsathe9779 Жыл бұрын
Great and in-depth explanation for every approach. I really liked this video. I have found another better approach final int n = s.length(); int len = 0; int [] repeat = new int[128]; for (int c = 0, j = 0, i = 0; j < n; j++) { c = s.charAt(j); i = Math.max(repeat[c], i); len = Math.max(len, j - i +1); repeat[c] = j+1; } return len; I somewhat understood, but it would be better if you can explain. Thanks John and keep creating more videos for different problems.
@ashashankar29
@ashashankar29 10 ай бұрын
Excellent explanation and solution. I clearly understood the problem and solution. thank you
@deepakroy3476
@deepakroy3476 2 ай бұрын
You make programming easy and simple
@hendhasnaoui9419
@hendhasnaoui9419 9 ай бұрын
i'm really impressed 👍👍. keep on going 👏👏👏
@rahulg1715
@rahulg1715 2 ай бұрын
At 35:00 we should use indexOfFirstAppearenceInsubstring
@thelazymim9338
@thelazymim9338 Жыл бұрын
Best Explanation. Please make videos on All LeetCode's 145 top interview questions. You will be immortal for the Computer Programmers Community.
@markmuyot2436
@markmuyot2436 7 ай бұрын
Hi John I like how you present your leetcode solutions, so easy to follow and understand. Hope to see more of this or if you have anothe platform teaching leetcode problems , I would be happy to know.. Thank you
@otisopoku37
@otisopoku37 5 ай бұрын
Your explanations are very good. More leetcode!!!!!!!!!
@gurjeevjohal5459
@gurjeevjohal5459 Жыл бұрын
You're videos are so helpful, one 5 minute video is worth 6 hours reading a chapter in a book. Please can you do some videos or a playlist on design patterns. Thanks!
@SimoLPers
@SimoLPers Жыл бұрын
I'm currently just at the brute-force method in the video, but for the lookups I'd probably use a hashset. I would therefore prevent an O(n²) lookup. But the brute-force method was the first idea for me as well.
@158thavashankarrajakc9
@158thavashankarrajakc9 Жыл бұрын
need more tutorials please :) thank you!
@Shivam-gh2mq
@Shivam-gh2mq Жыл бұрын
I watched till 25:32 and was able to come up with a solution. Thanks for this amazing explanation!
@adriangonzalez6564
@adriangonzalez6564 Жыл бұрын
Awesome video!!! You are by far the best coding tecaher i could find in internet. Coud you do a video explaining how to read a csv file in Java?? It would be great. Really very good content, keep doing it
@sheko4515
@sheko4515 3 ай бұрын
Can you please do more videos like this as you explain it in a great way.
@chinmayrath8494
@chinmayrath8494 Жыл бұрын
thanks for the video !!
@bendror8302
@bendror8302 11 ай бұрын
Nice to learn from you John, can you add more solutions from LeetCode?
@antonignatenko7776
@antonignatenko7776 Жыл бұрын
if possible more videos like this, especially interested in thought process behind solutioon
@psg9278
@psg9278 Жыл бұрын
Thanks Please do more
@logesh9141
@logesh9141 Жыл бұрын
hi John, The way of explanation which you are giving is excellent. Since, I am mostly working JAVA. You videos are very mich helpful for me to achieve greater heights in my life. I mostly use ECLIPSE for my Java Projects. If possible please guide me in using Intellij with each and every shortcuts. Thanks in Advance, Logesh
@jatinkumar7287
@jatinkumar7287 Жыл бұрын
Thanks for this explaination🥰
@EdgarRamirez-ry2je
@EdgarRamirez-ry2je 6 ай бұрын
More videos like this with solutions for diferent letcode, hackerank problems this is very much usefull to improve skills, you explain as a master
@Harsha._.
@Harsha._. Жыл бұрын
Awesome best explanation 👏
@dadaboymasharipov2653
@dadaboymasharipov2653 Жыл бұрын
Hi there!! I really like your videos so much you make them easy to understand!! I want to ask you something can you recommend a book or books to improve our problem solving skill and to learn data structures
@rcciit9226
@rcciit9226 8 ай бұрын
Wow so easy to learn and understand , you have a new student sir.
@aneeshdixit4
@aneeshdixit4 Жыл бұрын
Thank you for the amazing and clear explaination. Can you explain this next? 992. Subarrays with K Different Integers.
@jordanT_alker
@jordanT_alker 19 күн бұрын
Interesting! I am very honest with myself, I am not seeing the full picture when it occurs to recursion; However, I did grasp the idea of the "quicksort" method a bit better, not 100% but we are learning. I can see this is almost similar to an array.
@swagatpandak7325
@swagatpandak7325 Жыл бұрын
Continue this series in java please.Thanks john.
@CostaKazistov
@CostaKazistov Жыл бұрын
Yes, please! Most LeetCode videos out there are either Python or C++ Definitely want to see more LeetCode walkthroughs in Java (or Kotlin)
@bendror8302
@bendror8302 11 ай бұрын
wow thanks 🙏
@tiyoo6961
@tiyoo6961 Жыл бұрын
Thank you
@ozzDeveveloperOpenForWork
@ozzDeveveloperOpenForWork Жыл бұрын
perfect. you have more question answer videos, like a playlist of them? Thanks
@alexanderkomanov4151
@alexanderkomanov4151 Жыл бұрын
AMAZING!
@saeednoshadi3922
@saeednoshadi3922 Жыл бұрын
This playlist is amazing, Why don't you continue this series of videos?
@NiChOlAs-gw7iw
@NiChOlAs-gw7iw Жыл бұрын
wow pls, do more Leetcode exercise explanations!
@MTB_Bay_Area
@MTB_Bay_Area Жыл бұрын
Maybe HashMap takes longer because I the calculation of the hash? To get to a value HashMap first calculate hash value, and only then go to the location.
@laharibasu9731
@laharibasu9731 Жыл бұрын
Can you please make more of these leetcode java solutions videos. They are of great help and you make us understand wonderfully.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I will when I can!
@diegosuarezgarcia248
@diegosuarezgarcia248 Жыл бұрын
I'm assuming this last approach is faster because the window defined by 'left' and 'right' never ends up being long enough to operate less efficiently than when using a map. I mean, Imagine the 5*10^4 (50000) String where ALL its characters are different (this means, maxLength would be 50000). Everytime you call 'indexOf()', Java is basically running a O(N) algorithm, where N is the length of the window we use. Said window starts with length 0, it's increased each loop and its length ends up being 50000. We have a complexity O(50000!), don't we? For this kind of scenario, unless I am mistaken, using a map is a way better choice.
@manambalaji9547
@manambalaji9547 7 ай бұрын
Really you are awesome 🎉
@Verdarin
@Verdarin Жыл бұрын
I know this is a bit late, but I believe Map is quite slow in runtime from what I heard and so it doesnt give off the best speed, Map is a great to use if you dont care much about speed as much as you want cleaner and simple code (tho run time is and should be priority)
@germimonte
@germimonte Жыл бұрын
given that there's a finite and constant number of letters, you could probably replace the HashMap with a regular array for a negligible decrease in time and memory
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Yeah it could be the case that despite the technical time complexity being lower, because n will never be more than 50,000, simple sequential array searches might just be more performant
@cipherxen2
@cipherxen2 Жыл бұрын
@@CodingWithJohn you don't need array of 50000 elements, just 255 for ASCII characters
@bhaskarpurkayastha485
@bhaskarpurkayastha485 Жыл бұрын
@@cipherxen2 and a character can be found in this array in O(1) time by - int lastEncountered = charIndexes[currentChar]; Initialize charIndexes[255] with all -1;
@shakilansari8967
@shakilansari8967 Жыл бұрын
Lots of love from India...
@quandingleberry445
@quandingleberry445 Жыл бұрын
Great video, also I think the last part might be bc of best case scenario
@Laughing_india_
@Laughing_india_ Жыл бұрын
love from india❤️ Pls upload a problem on daily basis
@nicovogel9924
@nicovogel9924 Жыл бұрын
I would never thought of HashMap is it possible to do the same thing with a list just if list.contains(a) list.remove(a) list.append(a). Could you pls make a video about how fast all these datastructures in comparison are. you can explain very good :)
@VinuCP
@VinuCP 8 ай бұрын
Thanks John for awesome video and great explanation. For the Map solution wouldn't it also help to add an exit condition to break the loop if (maxLength > input.length() - right + 1) - so we need not iterate the remaining characters in the input.
@rishiraj2548
@rishiraj2548 Жыл бұрын
Thanks
@arthurmagamedov3179
@arthurmagamedov3179 9 ай бұрын
Thanks John for the amazing video!! As for maps - what is the low level implementation of maps in JVM code? I mean, does it calculate the hash for the key when put or get methods are invoked? If it does, then adding or reading keys will be quite expensive , and explains why indexOf works faster.
@jonathanhawkins9147
@jonathanhawkins9147 Жыл бұрын
Yum just a subscriber with the initial sponsor ad
@raygilbertflies
@raygilbertflies Жыл бұрын
Hello John, I have watched a lot of your videos and I like your teaching style. This is a lot off topic but I am currently trying to learn java. I have gone through iheritance and on to composition. I was really excited about composition and took the Vehicle/Car concept from inheritance and wrote a few more classes: Corvette, engine, transmission, accelerator. I made Corvette a composition of the four: Corvette vett = new Corvette(engine, transmission, accelerator, fuelTank) This works as long as the Corvette class does all the work because it has access to all the other classes. I am trying to find out if(itIsPossible) to make a composition within the composition so that as well as the car accessing the same engine, transmission and accelerator, the engine and transmission would likewise have access the same accelerator, the transmission and accelerator access the same engine and the accelerator accesses the same engine and transmission. So far, I have tried 27,354 different ways after looking at thousands of videos and forums,
@twokatana
@twokatana Жыл бұрын
Great Video! But what book would you recommend for learning java?
@germimonte
@germimonte Жыл бұрын
as for the better time, my best guess is a combination of the sample data and the relatively massive overhead of the default HashMap implementation, using arrays to implement your own map SHOULD in theory produce better results, I'll test it later. Or you could just pass 127 as the initial capacity of the map?
@MTB_Bay_Area
@MTB_Bay_Area Жыл бұрын
for the version that uses the HashMap, I would suggest to have an else. when we are in the "if contains" we are always shortening the substring and hence no need to update the max.
@rameezalam1968
@rameezalam1968 9 ай бұрын
Hi Johnny sin, good to see in as a software developer role as well
@bekuostudio
@bekuostudio 6 ай бұрын
Thanks for the clear explanation. I'm struggling with tech interviews. How am I supposed to complete a challenge in a short time? I just got stuck in the last one. I'm trying so hard to understand. I've been practicing LeetCode exercises, but there's always something tricky, and I've never seen anything similar before. It's so frustrating
@xeanluxcrille6847
@xeanluxcrille6847 Жыл бұрын
I'm not entirely sure, but the answer to the difference in time complexity might be in how indexOf is implemented in the first place. I'm not an expert at all, but what if the indexOf method itself already utilizes the exact same Map implementation? Meaning, your Map implementation could be a duplicate of indexOf(), except indexOf() doesn't have the extra steps within the implementations of contains() and get() because it's already encapsulated in core Java. I'm just guessing here, of course. Pretty sure I could be wrong.
@benjaminandersonii9842
@benjaminandersonii9842 Жыл бұрын
The reason for the final solution being faster is because while hash tables have "constant" lookup time it still takes time. Depending on the hashing function and the initial size of the hashmap the lookup, add, and resize functions may be more time consuming than just linearly looking through an array of characters. Especially if the hashing function has division, because of how bad computers are at it. So rather than your hashmap having O(1) time complexity for lookup its probably more like O(100) which is potentially smaller than the size of n. Sorry for the long rambling answer, but yeah hashmaps are great, if you know how the data is being put into them. They also take up a *bunch* of space, so finding an algorithm that is similar in functionality, but doesn't use them is always the way to go.
@vedantjha2327
@vedantjha2327 Жыл бұрын
True, just to add your answer, hashmap has constant time complexity in average case, but O(n) in worst case. So it's not 100% perfect n effective in all cases
@hasibulhasansiju
@hasibulhasansiju Жыл бұрын
Want more of this
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I'll definitely do more sometime soon!
@ALEXGAYMAR2312
@ALEXGAYMAR2312 Жыл бұрын
Could you please do one about Polymorphic load/save methods for Java Io file handling
@DominikGuzowski
@DominikGuzowski Жыл бұрын
The second solution is definitely better than the first one than the map purely because our domain is so small, so all of the map operations while "constant" probably take a tad bit longer than this linear search, but again this is just due to the domain. If this was not strings but instead an array of numbers, where the domain can be much larger, the map would probably outperform this approach, though that is just a guess.
@odaia.7519
@odaia.7519 Жыл бұрын
I think maps are very performant in java, so it's not because of the map functions. What i think it's because of initialize an object of it and this takes time. I saw this while measuring a function using map in my performance project.
@sysybaba420
@sysybaba420 7 ай бұрын
@31:42 -> is there a better way to find length of the substring(right - left + 1)? Can we do something like s.subString(left, right) and pass this as an argument to s.length() somehow and return the length of that substring? thoughts?
@sysybaba420
@sysybaba420 7 ай бұрын
Also at 30:00 why not just do a left++?
@james-ansley
@james-ansley Жыл бұрын
The max possible substring is bounded by the number of possible characters in the string, m, which given the problem constraints seems to be at most ~100ish. And, I would imagine on average the actual distance between the left and right pointers will be much less than this. This then means you are *effectively* comparing two linear solutions (even though the indexOf solution is technically nm - m is small). I would imagine that for small values of m the simplicity of linearly looping through a few characters of a string would be much faster than map lookups and manipulation. It would be interesting to see if the map-based solution would surpass the indexOf solution by increasing m, by, for example, using all UTF-8 chars.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Well the problem states that the input string may be up to 50,000 characters long. But I do think it's a good insight that the max length of the longest substring is bounded by the number of unique characters in the character set being used. In this case it says "English letters, digits, symbols and spaces." I'd have to add all those up to be sure, but let's say it's maybe 100, which may be a big reason the map solution is overkill at this level
LeetCode Exercise in Java Tutorial - Two Sum FAST Solution
23:36
Coding with John
Рет қаралды 162 М.
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 142 МЛН
¡Puaj! No comas piruleta sucia, usa un gadget 😱 #herramienta
00:30
JOON Spanish
Рет қаралды 22 МЛН
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 46 МЛН
Longest Repeating Character Replacement - Leetcode 424 - Python
20:44
Kademlia, Explained
24:22
number 0
Рет қаралды 14 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 75 М.
Java's Creators Rejected Multiple Inheritance - Here's Why
13:14
Coding with John
Рет қаралды 66 М.
Lambda Expressions in Java - Full Simple Tutorial
13:05
Coding with John
Рет қаралды 694 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Рет қаралды 142 МЛН