LeetCode 14. Longest Common Prefix Solution Explained - Java

  Рет қаралды 102,461

Nick White

Nick White

4 жыл бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow My Twitter - / nicholaswwhite
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 153
@atefe3919
@atefe3919 4 жыл бұрын
No one explains solutions better than you, thank you so much for these videos, they are helping me a lot in the process of getting ready for technical interviews!
@NickWhite
@NickWhite 4 жыл бұрын
thanks
@joanmoralesf
@joanmoralesf 2 жыл бұрын
Its insane how short this video is but you explained it better than all the long fancy videos. Thank you
@jay-wf3ft
@jay-wf3ft 3 жыл бұрын
I think it would be great if you would show 2 solutions. One with a slow run time and then the optimal solution such as this. It really helps to understand it even more. Either way awesome video
@pritampadhan5977
@pritampadhan5977 2 жыл бұрын
Above logic takes more time i think
@ananyadutta6358
@ananyadutta6358 3 жыл бұрын
bro your explaining method is quite creative , out of 7 videos based on this that I'd WATCHED , this was the best!
@vcfirefox
@vcfirefox 3 жыл бұрын
Dude, I am probably much older than you, but goddamn I am LOVING your explanations. Getting a chance to prep my interviews very efficiently!
@tanyagoel9140
@tanyagoel9140 4 жыл бұрын
It was so nicely explained. Thanks a lot!
@satyamgupta6030
@satyamgupta6030 Жыл бұрын
what a solution man thanks alot nick brother please continue making these videos. u r just different when it comes to explaining in most optimised ways.
@poojaupadhyay6364
@poojaupadhyay6364 3 жыл бұрын
such a clear approach! everytime i do leetcode and feel like wtf about a question ,i come here to watch your tutorial and i understand the solution so clearly! thanks a lot,u have a great help dude!
@suyogsubedi8584
@suyogsubedi8584 3 жыл бұрын
are you a better problem solver by now?
@bhargavvaddepally3221
@bhargavvaddepally3221 3 жыл бұрын
Damn man that explanation was so smooth
@prajwalshenoy9117
@prajwalshenoy9117 3 жыл бұрын
Nice one. We can have a case inside for loop to break it when prefix length reaches zero to avoid continuing to loop when there's no scope of common prefix further.
@saradham2844
@saradham2844 3 жыл бұрын
Best solution for this problem . Great approach and clear explanation Thanks !!!
@simrankak7045
@simrankak7045 3 жыл бұрын
Why i didnt find your channel until now. Your explaination is crisp clear and very simple thankyou :)
@KhayamGondal
@KhayamGondal Жыл бұрын
you can add a check for prefix length ==0 at the end of the while loop. Incase nothing matches with the 2nd string then no need to check with rest of the list. And instead of substringing every itteration you can keep track of right index and only substring when returning the result.
@raghavendrathakur2239
@raghavendrathakur2239 Жыл бұрын
@kshitijpandey9376
@kshitijpandey9376 2 жыл бұрын
You wrapped that up very smoothly. Thanks Nick🙂.
@greatred2558
@greatred2558 2 жыл бұрын
thanks, tried many things for c# but kept falling out of bounds. This works and simple to understand
@brucewayne9444
@brucewayne9444 4 жыл бұрын
Wow man. I could have never thought of approaching this problem this way. Thanks a ton👍
@nurulloabdurakhmanov4914
@nurulloabdurakhmanov4914 2 жыл бұрын
Exactly man. What should we do to get into thinking like these aproaches ?
@ashish4k07
@ashish4k07 7 ай бұрын
I thought of using 1st string in array of string as base and scanned through all others and checked if one by one it matches all other strings if it is then we increment the string and hence getting final lcp ans
@abhi26nav
@abhi26nav 3 жыл бұрын
Kudos to such a clean explanation ! Thank you.
@6066ChetanKhairnar
@6066ChetanKhairnar 3 жыл бұрын
got through 3 different videos for a good solution but nick is one who always gives the best approach , Appreciate bro
@utkarshsrivastava3114
@utkarshsrivastava3114 Жыл бұрын
What a clean and simple approach
@thomasgeorge4578
@thomasgeorge4578 2 жыл бұрын
Man! your explanation is just fire 🔥🔥🔥
@flyfly11111
@flyfly11111 2 жыл бұрын
You explained it very nicely. Thank you so much
@AlejandroRodriguez-lq9mz
@AlejandroRodriguez-lq9mz 4 жыл бұрын
thanks for sharing such solution, very clear and easy one!
@hbrymiri8301
@hbrymiri8301 Жыл бұрын
Hands best step by step explanation.
@jiangtianfeng5971
@jiangtianfeng5971 4 жыл бұрын
very clear! Thx man
@tej3679
@tej3679 Жыл бұрын
best explanation, you explained clearly kudos
@himanshumalik6913
@himanshumalik6913 2 жыл бұрын
You know what, you just explained in a so easier way that no one has explained yet.
@LeetCodeSimplified
@LeetCodeSimplified 3 жыл бұрын
Great explanation! Thx a lot!!
@amitpadaliya6916
@amitpadaliya6916 4 жыл бұрын
really amazing solution
@prathamchitransh5338
@prathamchitransh5338 2 жыл бұрын
best explaination for this problem on youtube
@headyshotta5777
@headyshotta5777 Жыл бұрын
Thanks for the explanation. I still don't really understand how your while loop works but I'll keep working to understand.
@DevanshuMevada
@DevanshuMevada 4 жыл бұрын
Thanks Man!
@solomonmokua4643
@solomonmokua4643 Жыл бұрын
Than you, well explained, was able to implement your solution in Python.
@aadhi_dinesh7668
@aadhi_dinesh7668 Жыл бұрын
IN PYTHON s=["flow","flew","flower"] k=s[0] flag=0 a="" for x in range(len(k)): for j in s[1:]: if k[x]!=j[x]: flag=1 else: a=""+k[x] if flag==1: break else: print(a,end="")
@rohitkushwaha6125
@rohitkushwaha6125 2 жыл бұрын
Thank you so much for the explanation ut was really nice.
@user-uh7lr6bo7w
@user-uh7lr6bo7w Жыл бұрын
Amazing explanation!
@Post2prqbu
@Post2prqbu 4 жыл бұрын
wow! elegant solution!
@vicente3j
@vicente3j Жыл бұрын
When I search up a leetcode question I always go to Nick White if its available 😂 Great video
@zerosandones7547
@zerosandones7547 2 жыл бұрын
confusion.Cleared(); THANK YOU!
@kso3586
@kso3586 Жыл бұрын
Amazing! thank you so much👍
@vidhi_bhatt
@vidhi_bhatt Жыл бұрын
simple and on point!
@gokulkumarbhoomibalan5413
@gokulkumarbhoomibalan5413 3 жыл бұрын
Really helpful!
@freinheit6923
@freinheit6923 10 ай бұрын
thank you for explaining!
@NeeruSinghK
@NeeruSinghK 4 жыл бұрын
great.. nice and easy solution
@adil_k
@adil_k 10 ай бұрын
Thanks for this awesome video
@ruifan793
@ruifan793 3 жыл бұрын
Help me a lot, thanks
@KIM-ODee
@KIM-ODee Ай бұрын
Thanks for your explain!
@jasonliu7186
@jasonliu7186 4 жыл бұрын
Thanks very much!
@ricardosmith5753
@ricardosmith5753 2 жыл бұрын
very clean solution.
@habibdurodola5728
@habibdurodola5728 3 жыл бұрын
Best solutions , Yes you did !
@srijitdebsarma3451
@srijitdebsarma3451 Жыл бұрын
Best explanation 😍🥰
@shivamd23
@shivamd23 3 жыл бұрын
Best Solution 😊👍
@Ben-pb7ct
@Ben-pb7ct 3 жыл бұрын
Could you explain why the time complexity is O(S), where S is the sum of all characters in all strings?
@swagatzeher
@swagatzeher 3 жыл бұрын
Thanks man Nick... you are a pocket sized dynamite ;)
@ayushbudhwani
@ayushbudhwani 3 жыл бұрын
Nailed it!!
@vrindatyagi6744
@vrindatyagi6744 3 жыл бұрын
THANKYOU SO MUCH
@sreenidhisreesha
@sreenidhisreesha Жыл бұрын
Since indexOf is linear time complexity, Doesnt it make this O(n2) time complexity?
@GoodTimeDev
@GoodTimeDev Жыл бұрын
Thank you !
@crackonmynikes681
@crackonmynikes681 Жыл бұрын
amazing tutorial
@theolim6994
@theolim6994 Жыл бұрын
Thank you so much for great explanation!!! Helped so much!
@abdulqudusyunus9786
@abdulqudusyunus9786 2 жыл бұрын
Nice explanation
@mohammadosama4709
@mohammadosama4709 3 жыл бұрын
Why do we need substring method we can check character by character vertically? Wouldn't that me more optimal?
@ehsanhosseini5861
@ehsanhosseini5861 3 жыл бұрын
Thank you for this amazing explanation. What is the time complexity?
@royt6924
@royt6924 3 жыл бұрын
I believe it is O(A*S) where A is the iteration for each element of array, where S for the iteration of string
@blaze9558
@blaze9558 9 ай бұрын
thanks man!
@thatsenoughdixit
@thatsenoughdixit Жыл бұрын
Very nice. Commenting so I get more videos.
@balavardhanreddy8581
@balavardhanreddy8581 Жыл бұрын
by watching this video i subscribed ur channel
@xchen7499
@xchen7499 3 жыл бұрын
Thank you, well explained, and very helpful!
@arjun-cw7cr
@arjun-cw7cr 4 жыл бұрын
thank you
@simardeepkaur8275
@simardeepkaur8275 2 жыл бұрын
Thanks buddy
@carefree_ladka
@carefree_ladka Жыл бұрын
indexOf() takes O(N) time . How is your method linear?
@akhila5597
@akhila5597 3 жыл бұрын
Simply wow
@harshal101096
@harshal101096 2 жыл бұрын
whats the timecomplexity?
@maxstanley1818
@maxstanley1818 3 жыл бұрын
yo but what if the first element in the "fl" prefix array is something like "dog" or something?
@Itachibatman
@Itachibatman Жыл бұрын
thanks don
@sophiezhang6516
@sophiezhang6516 4 жыл бұрын
thank u~
@mohitdangwal587
@mohitdangwal587 2 жыл бұрын
you the BEST!!!!!!!!!!!!!!!!!
@gayan1742
@gayan1742 2 жыл бұрын
Time complexity of indexOf is O(n) though.
@fei3841
@fei3841 4 жыл бұрын
thanks!!
@longchen4072
@longchen4072 4 жыл бұрын
clever man!
@pragatikumarprusty7061
@pragatikumarprusty7061 2 жыл бұрын
If possible, please tell the time and space complexity as well ...
@fahdagodzo5795
@fahdagodzo5795 3 жыл бұрын
This guy is smart
@isaacwhiz
@isaacwhiz Күн бұрын
You a genius
@ashish4k07
@ashish4k07 7 ай бұрын
So can what can be the overall time complexity for this
@davidc3268
@davidc3268 2 жыл бұрын
how is a linear solution? theres is a nested while loop inside?
@Whaleshamu
@Whaleshamu 4 жыл бұрын
can you make video on Gray code (Leetcode 89)?
@NickWhite
@NickWhite 4 жыл бұрын
I’ll check it out!
@gauravmishra8782
@gauravmishra8782 9 ай бұрын
how did you came up with this solution?
@abhinavs2484
@abhinavs2484 Жыл бұрын
in worst case it is O^n2 right, is there any better solution!
@dgr8raka470
@dgr8raka470 Жыл бұрын
I think in place of “indexOf” .. we need to use “startswith” method… tq for the vdo
@velmuruganjeyakumar1746
@velmuruganjeyakumar1746 Жыл бұрын
Yooo Man , I too had that same feeling
@Noname-di3yz
@Noname-di3yz Жыл бұрын
the only thing I don't understand is why we can set the first element as the prefix initially... isn't it possible that there is a different longest prefix? ex/ ["ab", "cd", "cde", "cdef","cdefg"], where if we set the prefix to "ab" wouldn't your solution return "" ?
@syc4654
@syc4654 Жыл бұрын
probably very late but although the problem doesn't mention it specifically,longest common means that the common prefix must be common for all the elements in the array,so yes,"" would be returned and it would be correct,you can test it yourself inputting your string own string in the Testcase section(In the console) and seeing the expected resulted (from a working code) and your result (from your code)
@chloetang211
@chloetang211 Жыл бұрын
Oh, so prefix means it has to be started at 0. After I knew this, this question became simple. Thank you for your great explanation! love it. Your method is way better than those complex long solutions which I couldn't understand
@sameer9368
@sameer9368 4 жыл бұрын
hello....please do the problem on the merging k- sorted array using heap.
@NickWhite
@NickWhite 4 жыл бұрын
send me a link in discord and i'll check it out
@sameer9368
@sameer9368 4 жыл бұрын
i am solving in eclipse . i have the problem statement. can i share you that problem statement. i dont know the approach how to solve such problem, i am the beginner. also i am unaware of the concept like heap. please do the video on it
@avinashpatil9662
@avinashpatil9662 Жыл бұрын
What is the time complexity of this solution?
@start1learn-n171
@start1learn-n171 Ай бұрын
Tq
@TheRishit99
@TheRishit99 2 жыл бұрын
Great explanation. Can solve this with Trie Tree ?
@carefree_ladka
@carefree_ladka Жыл бұрын
That's the efficient way to solve this problem
@shastriswaroop
@shastriswaroop 4 жыл бұрын
How about setting the prefix as the smallest length string and then starting loop from index 0. For longer string array it will be beneficial?
@swethanooka896
@swethanooka896 2 жыл бұрын
Can anyone explain me the while loop part of the code?
@pjguitar15
@pjguitar15 2 жыл бұрын
I literally got stuck to this problem and I just gave up and went to KZfaq lol
@iTzAlwxyss
@iTzAlwxyss 2 жыл бұрын
How does this not work for processing 4 that uses java ?
@maxh8190
@maxh8190 Жыл бұрын
HoLy what a smart approach
@rajeevsingh5453
@rajeevsingh5453 3 жыл бұрын
What's the time complexity of this algorithm?
@aryamankukal1056
@aryamankukal1056 2 жыл бұрын
linear
@abhishekmamgain224
@abhishekmamgain224 2 жыл бұрын
Still you are using While loop and one for-loop. Wouldn't this make the time complexity similar as two for-loops
@mohammadrafi895
@mohammadrafi895 Жыл бұрын
No, it wouldn't because the inner loop will not depend on the length of input array. Input array could have 100 words but each word can't be longer than 20 characters, on average 5-10.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 323 М.
HOW DID HE WIN? 😱
00:33
Topper Guild
Рет қаралды 41 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 537 М.
Alat Seru Penolong untuk Mimpi Indah Bayi!
00:31
Let's GLOW! Indonesian
Рет қаралды 16 МЛН
你们会选择哪一辆呢#short #angel #clown
00:20
Super Beauty team
Рет қаралды 52 МЛН
LeetCode 238. Product of Array Except Self (Solution Explained)
14:49
Longest Common Prefix
10:35
Kevin Naughton Jr.
Рет қаралды 76 М.
LeetCode 3Sum Solution Explained - Java
10:00
Nick White
Рет қаралды 195 М.
Longest Common Prefix - Leetcode 14 - Python
6:31
NeetCode
Рет қаралды 154 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 617 М.
How To Solve Algorithms - Longest Common Prefix
9:31
Web Dev Simplified
Рет қаралды 29 М.
Longest common prefix
14:24
Techdose
Рет қаралды 79 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Easy Art with AR Drawing App - Step by step for Beginners
0:27
Melli Art School
Рет қаралды 11 МЛН
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 13 МЛН
Samsung Galaxy Unpacked July 2024: Official Replay
1:8:53
Samsung
Рет қаралды 23 МЛН
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 6 МЛН
Опыт использования Мини ПК от TECNO
1:00
Андронет
Рет қаралды 770 М.