How To Solve Algorithms - Longest Common Prefix

Algorithms are difficult to understand, but absolutely crucial for landing a job. In this video I will be explaining the longest common prefix algorithm in depth. I will cover both of the most common approaches to the algorithm and comparing them.
I will walk through animated examples of the algorithm to explain the reasoning behind why the different approaches work, and then at the end of the video I will program the algorithm using JavaScript using the most optimal solution.
📚 Materials:
GitHub Code: github.com/WebDevSimplified/J...
CodePen Code: codepen.io/WebDevSimplified/p...
LeetCode Page: leetcode.com/problems/longest...
🧠 Concepts Covered:
- Measuring time complexity
- How to loop over string characters
- How to analyze an algorithm
- Efficient ways to parse arrays
- How to analyze a problem
🌎 Find Me Here:
Twitter: / devsimplified
GitHub: github.com/WebDevSimplified
CodePen: codepen.io/WebDevSimplified
#Algorithm #JavaScript #LongestCommonPrefix

Пікірлер: 109

  • @jeremyzhang8603
    @jeremyzhang86033 жыл бұрын

    This is the nicest explanation I have ever had! Please do more leetcode problems like this!

  • @jordantalbot8584
    @jordantalbot85845 жыл бұрын

    Thank you for the content man. Your channel is so underrated. This is my go to channel now. Keep it up man and your channel will blow up! Like everybody said use the playlist feature for similar content it makes a huge difference.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thanks! I have these algorithm videos already in a playlist and have many playlists for my similiar content on my channel.

  • @Sonoioilmago
    @Sonoioilmago5 жыл бұрын

    Love this series! I'd love to see more of the thought process and what clues to look for in the challange description to structure the code. I'm a beginner and sometimes struggle with my weird thought processes! Thank you for the great vid!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    That is a great suggestion. I have another algorithm video coming out soon, so hopefully I explained it a bit more in depth for you.

  • @ROotHM
    @ROotHM3 жыл бұрын

    These are amazing, and I'd love to see your breakdown of all the major sorting algorithms - along with a brief explanation of when it would be best to implement these types of sort

  • @mohammedsadiq5729
    @mohammedsadiq57294 жыл бұрын

    Man you should continue with this series ❤️ You are awesome!!

  • @mandihaase2744
    @mandihaase27442 жыл бұрын

    I absolutely love this series!

  • @JoeWong81
    @JoeWong815 жыл бұрын

    Thanks a lot bro! love your no nonsense teaching style. Please do more leetcode walkthroughs in the future.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I plan to keep covering them. They are fun to make.

  • @obasajujoshua9860
    @obasajujoshua98604 жыл бұрын

    This is great man, thank you so much for this.

  • @avagsargsyan404
    @avagsargsyan404 Жыл бұрын

    Thank you! Great solution!

  • @miw879
    @miw8792 жыл бұрын

    Thank you soo much for making this tutorial This helped me alot!! You are the best!!

  • @Lads-
    @Lads-5 жыл бұрын

    thank you so much! love this series please make more of algorithms series!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I plan to make quite a few more.

  • @crestmarinaris6113
    @crestmarinaris61135 жыл бұрын

    Thank you for the video =) always look forward to these algorithm series.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    You're welcome!

  • @pushpakgupta7396
    @pushpakgupta73965 жыл бұрын

    please make a series of this and never stop....thanks for everything. God bless you

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I have a couple algorithm videos and plan to make more.

  • @shuvajitmohanty9595
    @shuvajitmohanty95955 жыл бұрын

    You have "simplified" this algo with ease....Thank you...Wish for these kind of videos on algo everyday

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I wish I had enough time to do that.

  • @shuvajitmohanty9595

    @shuvajitmohanty9595

    5 жыл бұрын

    @@WebDevSimplified Hahaha

  • @ActiveZeni
    @ActiveZeni5 жыл бұрын

    Have a series going through most of leetcode algo problems (easy/medium difficulties)! Great video!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    That's the goal. I'm glad you enjoy them.

  • @nora8200
    @nora82003 жыл бұрын

    Thank you for this awesome video!

  • @PortEXE
    @PortEXE5 жыл бұрын

    Great video! KZread needs more of these algorithm videos.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thanks!

  • @vladimirjean
    @vladimirjean5 жыл бұрын

    Awesome tutorials like always!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thanks!

  • @manishbhatt1101
    @manishbhatt11015 жыл бұрын

    Thank you Web Dev Simplified. This is good!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    You're welcome!

  • @hikemalliday6007
    @hikemalliday6007 Жыл бұрын

    well said and good audio quality

  • @BrendanMetcalfe
    @BrendanMetcalfe4 жыл бұрын

    Just subbed. Your channel is gold!

  • @justcodingandwhatnot9890
    @justcodingandwhatnot98902 жыл бұрын

    thank you this helped alot !!

  • @MrWolfgangWeiss
    @MrWolfgangWeiss4 жыл бұрын

    Well done, great explanation!

  • @WebDevSimplified

    @WebDevSimplified

    4 жыл бұрын

    Thank you

  • @oop_web7707
    @oop_web77075 жыл бұрын

    Thank you so much. This was very useful

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I'm glad you enjoyed it!

  • @akamfoad
    @akamfoad4 жыл бұрын

    That was nice problem to solve and great explanation from you, I’ll skip the code and write it by mslf🤍

  • @janezklun
    @janezklun5 жыл бұрын

    Great topic and nice solution, Thanks

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    You're welcome!

  • @praniltunga
    @praniltunga4 жыл бұрын

    Nice content and covered in a very nice way 🙏👍 thanx man🙏

  • @WebDevSimplified

    @WebDevSimplified

    4 жыл бұрын

    You're welcome!

  • @BrendanMetcalfe
    @BrendanMetcalfe4 жыл бұрын

    Well done!

  • @4541047
    @45410475 жыл бұрын

    I love your channel man! You are a pro, and your explanations are the best. Can you please make a video on notifications in PWA? Thanks

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I plan to eventually jump into some PWA videos but I still have a lot of topics I want to cover first.

  • @KhoaNguyen-mv2mu
    @KhoaNguyen-mv2mu4 жыл бұрын

    beautiful amazing fantastic mind-cracking idea :))

  • @manikumarponnuru7271
    @manikumarponnuru72713 жыл бұрын

    I subscribed your channel after watching the first video. I would love to listen more of these algorithms from you. if possible can you make the video on Binary Search and Binary Tree?

  • @KallyanBRoy
    @KallyanBRoy4 жыл бұрын

    Dude, you are a true 'Algorithmist'🥰🥰

  • @omerm80
    @omerm80 Жыл бұрын

    Thank You! just subscribed to your channel:)

  • @chrisdanikas7918
    @chrisdanikas79185 жыл бұрын

    Great video dude!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thanks!

  • @mareimorsy3182
    @mareimorsy31825 жыл бұрын

    Thanks a lot .... Best explanation ever!!! :D

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I'm glad you enjoyed it!

  • @parasarora5869
    @parasarora58695 жыл бұрын

    great one!!!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thanks!

  • @qwertykeypad789
    @qwertykeypad7895 жыл бұрын

    You are doing great!!! Please give as a full in-depth tutorial on MongoBD And use some live example to use it on project

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    That is on my list of todos. I have a Node.js MongoDB course that is free on KZread and covers Node.js and MongoDB in depth if you want to check that out.

  • @tsukasa8179
    @tsukasa81792 жыл бұрын

    Legendary

  • @shreyasd4663
    @shreyasd46635 жыл бұрын

    Just subscribed! The more you will share algorithms video the 10x more I am gonna share your algorithm video links with internet!

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I'm really glad you enjoyed it.

  • @shreyasd4663

    @shreyasd4663

    5 жыл бұрын

    @@WebDevSimplified they only topic that doesn't put dust on internet is PROBLEM SOLVING. It's a win-win thing. You learn more you grow; you share it on KZread channel grows! Do share and make it happen! ;)

  • @eliotlnguyen7329
    @eliotlnguyen73294 жыл бұрын

    You're dope, brother.

  • @yesweet
    @yesweet5 жыл бұрын

    I hope you could kindly show us how you create the video, all your technics help understanding while keeping all clean, very impressive, few can compete with you on this point. 👍

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Thank you. What do you mean by showing you how to create the video?

  • @ikramikky2838
    @ikramikky28385 жыл бұрын

    thank you very much

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    You're welcome!

  • @kumaresanperumal2581
    @kumaresanperumal25813 жыл бұрын

    Does this program use Big O notation?

  • @vishalscit
    @vishalscit4 жыл бұрын

    Quick question.Shouldn't j loop start from 1 though ? Thanks for your videos dude.

  • @herrklaus

    @herrklaus

    3 жыл бұрын

    Smart! Saves him the time of running it against the string he's already using as a reference.

  • @krishy9661
    @krishy96615 жыл бұрын

    Hi, Great explanation. Can you please make a video of how to solve Maximum sum sub array problem using Kadane's Algorithm in Javascript?

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I can look into it

  • @AhSoh7091
    @AhSoh70915 жыл бұрын

    what is the pregress of two for loop in nested. Is the frist loop fn only execute one time then going into second loop fn, after second loop is finished* condition finished then return back to first loop for second time of first loop fn then go second loop fn again? hope helper can understand what i want to said =)

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    That is exactly what happens. Good analysis.

  • @scenesofworld1066
    @scenesofworld10665 жыл бұрын

    Also need php tutorials .great work

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    I probably won't be doing much PHP on this channel since I do not enjoy it as much and it is much less popular.

  • @pawanvora
    @pawanvora5 жыл бұрын

    I am no JS expert, but I was wondering if the inner loop should start with j=1... since j = 0 would refer to the same string as the outer loop. Thanks.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    That is correct. Good catch!

  • @diegocastro7434

    @diegocastro7434

    3 жыл бұрын

    I was thinking of this too but that would mess up the case if there was only one string. Great idea but you also would need to add if len(strs)==1 return strs[0]

  • @samartajshaikh2601
    @samartajshaikh26014 жыл бұрын

    it would be better if you had also covered the divide-and-conquer approach and binary-search approach to solve this problem.

  • @kousalyadevi5209
    @kousalyadevi5209 Жыл бұрын

    Make a video on Longest Common "Substring" for "more than two strings"

  • @dim_1074
    @dim_10742 жыл бұрын

    You could also use a trie.

  • @memeta100
    @memeta1003 жыл бұрын

    Thx you got subscription by me

  • @primordialmind
    @primordialmind Жыл бұрын

    There is a bug in the code actually. For example, strs = ["ab", "a"] will throw an index out of range error, We are comparing all indexes of strings with string 1, which in our case is "ab". Second string is of length 1. So, accessing the index 1 will produce an error.

  • @woto6090

    @woto6090

    Жыл бұрын

    Yes, that's why you should sort the list with key=len first. Now you won't run in to index errors because you have the shortest member of the array.

  • @primordialmind

    @primordialmind

    Жыл бұрын

    @@woto6090 Why add O(nlogn) overhead, when you can make it work by slight if/else checks?

  • @Benescoli
    @Benescoli4 жыл бұрын

    I can't do that, even you explain me, i guess that i have to really train in algorithmes

  • @michaelrooze278
    @michaelrooze278 Жыл бұрын

    What if the first word is a million characters long though? Should we first sort the words by length, or would that be even worse space-time complexity?

  • @tannerbarcelos6880
    @tannerbarcelos68803 жыл бұрын

    Vertical method is very intuitive.

  • @pushpakgupta7396
    @pushpakgupta73965 жыл бұрын

    more algorithm videos pls

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Will do!

  • @eliotlnguyen7329
    @eliotlnguyen73294 жыл бұрын

    You have such a great demeanor. Keeeeeeep it up, bro. Maybe just get a light for your face, lol.

  • @luojihencha
    @luojihencha4 жыл бұрын

    And binary search method man?

  • @jb160
    @jb1605 жыл бұрын

    can you do it with java,there is solutions but not realy explain what they did

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    You can definitely modify this to use Java. The concepts explained in this video apply to any programming language and the only difference is the syntax of JavaScript vs Java.

  • @BrendanMetcalfe
    @BrendanMetcalfe4 жыл бұрын

    A python implementation people may find helpful. class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: longestPre = "" if strs: for i in range(len(strs[0])): char = strs[0][i] for j in range(len(strs)): try: if strs[j][i] != char: return longestPre except: return longestPre longestPre = longestPre + char return longestPre

  • @DanielLee-rr2qj
    @DanielLee-rr2qj4 жыл бұрын

    Anyone know the run time complexity of this solution? It's O(n^2) isn't it? I feel like there HAS to be a better way of doing this..

  • @WebDevSimplified

    @WebDevSimplified

    4 жыл бұрын

    If you go to the leetcode link in the description they have breakdowns of all the different solutions and time complexities. While this is not the fastest method it is significantly easier and not much slower.

  • @DanielLee-rr2qj

    @DanielLee-rr2qj

    4 жыл бұрын

    @@WebDevSimplified Ahh got it. Thank you!

  • @arunvijay2279
    @arunvijay2279 Жыл бұрын

    ["ab", "a"] = above code will fail for this case

  • @vinamra4893
    @vinamra48935 жыл бұрын

    There is a bug here. Suppose the String[] strs = [apple, ape, a] Starting with i = 0, 'a' is added to prefix. (BUG HERE) Then i = 1, here there will be an ARRAYOUTOFBOUND for i = 1 , j = 2 ...strs[j][i] for last string 'a'.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    Javascript does not have array out of bounds errors. It just returns undefined if there is nothing at that index of the array.

  • @vinamra4893

    @vinamra4893

    5 жыл бұрын

    Oh my bad! I used Java to program this and found it might give error. Thanks for reply and making this video! Otherwise I would have not understood concept of vertical scanning.

  • @WebDevSimplified

    @WebDevSimplified

    5 жыл бұрын

    @@vinamra4893 No worries. I am really glad you enjoyed the video.

  • @usama2576
    @usama2576 Жыл бұрын

    Your solution won't work for ["ab", "a"]

  • @snowsol
    @snowsol3 жыл бұрын

    A *P P* L

  • @jankidepala
    @jankidepala4 жыл бұрын

    hair is so distracting :P

  • @garrethhynes1201
    @garrethhynes12012 жыл бұрын

    This is working for me on Leetcode but when I copy it into VS code it returns 'undefined' when I pass in the array - const test = ["flower","flow","flight"] console.log(longestCommonPrefix(test)) Has anyone got any idea why this might be? Thanks and great video!!

  • @primordialmind

    @primordialmind

    Жыл бұрын

    There is a bug in the code actually. For example, strs = ["flower", "flow", "flight"] will throw an index out of range error, We are comparing all indexes of strings with string 1, which in our case is "flower". Second string is of length 4. So, accessing the index 4 will produce an error.