Python Programming Practice: LeetCode #14 -- Longest Common Prefix

Фильм және анимация

In this episode of Python Programming Practice, we tackle LeetCode #14 -- Longest Common Prefix.
Link to the problem here:
leetcode.com/problems/longest...
If you don't know Python, you can learn the basics of Python for data analysis using this guide I created on Kaggle (DataDaft video series forthcoming): www.kaggle.com/hamelg/python-...
Python Programming Practice is a series focused on teaching practical coding skills by solving exercises on popular coding websites. Note that the solutions seen here may not be the most efficient possible.
I am not going to provide the full code in the video description for this series, since copy and pasting solutions is not in the spirit of doing coding exercises. It is intended that the video will help you think about problems, approaches and how to structure solutions so that you are able to code up a working solution yourself. .
⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: www.kite.com/get-kite/?...

Пікірлер: 30

  • @saichander2314
    @saichander23143 жыл бұрын

    Short and crisp solution 👍

  • @brovet78
    @brovet784 жыл бұрын

    Nice solution! easy to understand

  • @son0funiverse
    @son0funiverse2 жыл бұрын

    Great solution!

  • @nitinshukla4960
    @nitinshukla49603 жыл бұрын

    THANKS man Appretiated

  • @ihavecrappyvids
    @ihavecrappyvids4 жыл бұрын

    best explanation tyvm

  • @-rahul-2908
    @-rahul-2908Ай бұрын

    THANKS SIR! :)

  • @laax
    @laax3 жыл бұрын

    Great video! What about time and space complexity? I have simplified a bit your solution: class Solution(object): def longestCommonPrefix(self, strs): if len(strs)==0: return("") if len(strs)==1: return(strs[0]) pref = strs[0] plen = len(strs[0]) for s in strs[1:]: while pref != s[:plen]: pref = pref[:-1] plen = len(pref) if plen ==0: return("") return pref

  • @tl8035
    @tl80353 жыл бұрын

    thank you! what would be the time complexity for this since the while loop might shorten each time? still O(N^2)?

  • @GZAMORA27

    @GZAMORA27

    3 жыл бұрын

    It could be O(n*m) where N is the number of strings in the array and m is the number of times it has to repeat the while (there should be a better way to name m)

  • @kentsang9376
    @kentsang93763 жыл бұрын

    best explanation

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

    wonderful😀

  • @tejaspoojary5397
    @tejaspoojary53973 жыл бұрын

    easiest explanation! keep it up

  • @DataDaft

    @DataDaft

    3 жыл бұрын

    Glad it helped!

  • @SHSelect
    @SHSelect2 жыл бұрын

    Thank you for the video. Question: what if the array was ['common', ' desend', 'decent'], would the codes find out the longest prefix which is "de"?

  • @dossymzhankudaibergenov8193

    @dossymzhankudaibergenov8193

    2 жыл бұрын

    nope, there is not common prefix

  • @DeadPool-jt1ci
    @DeadPool-jt1ci3 жыл бұрын

    while pref != s[0:plen] , what happens if pref is contained in s , just not in the [0:plen] portion ? , for example (car,teslacar) , u would have that car != teslacar[0:3] , and then shorten car into ca , yet car is a common prefix

  • @juanmoscoso9573

    @juanmoscoso9573

    3 жыл бұрын

    prefix is at the beginning of the word

  • @JohnDoe19754
    @JohnDoe197543 жыл бұрын

    Clear and nice explanation, can you please tell the time complexity?

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

    What is space and time complexity here?

  • @igorverevkin7709
    @igorverevkin77093 жыл бұрын

    Could somebody please explain this line: "while pref != s[0:plen]". What exactly does this condition mean?

  • @prithvimd9091

    @prithvimd9091

    2 жыл бұрын

    Instead of checking the whole string characters of s, it checks only for the length of the prefix.

  • @ryanmanchikanti5265
    @ryanmanchikanti52653 жыл бұрын

    is this a brute force way to search for the elements or a different technique?

  • @harshitbhatt5875

    @harshitbhatt5875

    3 жыл бұрын

    former

  • @damircicic
    @damircicic3 жыл бұрын

    What if the second string in the array is longer than the first? Then common prefix could be longer than the pref, and you would have to insert addition. On the other hand, if the array of strings is sorted so that strings are getting shorter as their index is bigger, than line 14 would give you an Out of Range error, because plen would be bigger than the len(second_string). Finally, if there is no common prefix between first and the second string in the array, plen should be 0 and search would stop because of the return command. And that would likely give a wrong result. But, according to LeetCode everything is OK. What is going on here?

  • @JohnDoe19754

    @JohnDoe19754

    3 жыл бұрын

    Hi, please find the below explanation 1. If the second string is longer than the first, we would not add anything to the prefix, since we have to find 'common' present in all three and it cannot be bigger than the shortest string in the array. So even if the length of the second is greater, the prefix will still be the shorter one since it's present in both. 2. Python does not give you an error if you try to slice up a string after it's the length. Check the output of this. If a ='flower' then 'print(a[0:20])' will not give an error instead just print flower. 3. If there is no prefix we are using return("") i.e an empty string . It does not give a wrong answer.

  • @OverdoseGamingPubg

    @OverdoseGamingPubg

    Жыл бұрын

    for example if in list ['l'.'love''lost.'life'] so the common prefix is only 'l' so we do not need a long word to check

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

    class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs)==0: pref='' else: L=[] for text in strs: num=len(text) L.append(num) count=min(L) for j in range(count+1): S = [] for text in strs: t=text[0:j] S.append(t) if len(set(S))

  • @us8314
    @us83143 жыл бұрын

    an easy solution but not efficient

  • @DataDaft

    @DataDaft

    3 жыл бұрын

    Sometimes easy is the most efficient.

  • @juanmoscoso9573

    @juanmoscoso9573

    3 жыл бұрын

    how would you make it more efficient?

Келесі