Find the Length of the Longest Common Prefix - Leetcode 3043 - Python

Поделиться
HTML-код
  • Опубликовано: 25 янв 2025

Комментарии • 36

  • @srivanthsai
    @srivanthsai 4 месяца назад +10

    "I don't know what they were smoking" that got me ROFL

  • @vandit_quantum
    @vandit_quantum 4 месяца назад +2

    Bro! The walkthrough was great! Boosted my confidence

  • @ruslooob
    @ruslooob 4 месяца назад +3

    i solved this problem with trie. thank for you solution!

  • @wwoosh7147
    @wwoosh7147 4 месяца назад +3

    This one just came up in my OA today, only if I had saw this earlier

    • @samuelhurh1481
      @samuelhurh1481 4 месяца назад

      Same unfortunately. had settle for half credit with brute force.

  • @sidazhong2019
    @sidazhong2019 7 дней назад

    A Trie is specifically designed for searching prefixes. The challenge lies in its unique data structure(linklist + hashmap). If you can remember the structure and how to use it, you'll have a breakthrough. The algorithms for a Trie itself are incredibly simple.

  • @Adarshhb767
    @Adarshhb767 4 месяца назад +3

    Sets already do not contain duplicate right

    • @NeetCodeIO
      @NeetCodeIO  4 месяца назад

      Yeah that's right, but it's still worth checking because then we can stop the loop early.

  • @Philgob
    @Philgob 4 месяца назад +2

    LFG right when i needed it

  • @quirkyquester
    @quirkyquester 2 месяца назад

    amazing approach, thx!

  • @jamestwosheep
    @jamestwosheep 4 месяца назад +1

    I found a solution that involved sorting both arrays lexicographically, doing a prefix comparison between the pairs starting with pointers at the beginning of both arrays, and then incrementing the pointer of the number that was smaller. I think the time complexity of that would be O(mlog(m) + nlog(n)), but space complexity would be O(1). Interestingly though, I got a faster run time doing it this way, but that may have been more to do with the luck of the run.

    • @visase2036
      @visase2036 4 месяца назад +1

      Technically, with tries we do the same ! Helps us to sort the values lexicographically and move the trie ptr to the subsequent trie objs if there is a match by tracking the longest number with the match.

  • @howardlam6181
    @howardlam6181 4 месяца назад

    In the real world, you probably have a SQL server for querying string match so the hashmap solution is in fact one of the ways to design the database for partial string search.

  • @pastori2672
    @pastori2672 4 месяца назад

    the trie solution is more intruitive after the recent problems, but yeah your right the hs solution is more efficient and the editorial is kinda miss-leading and has some typos i think they just don't care anymore

  • @pastori2672
    @pastori2672 4 месяца назад

    bro PLEASE tell me what you used for that dsa tree on your website you did mention it once but i forgot what it was

  • @flp508
    @flp508 4 месяца назад

    What position did you apply for in Google?

  • @VanjeAv
    @VanjeAv 4 месяца назад

    Great explanation

  • @ROHANMANNA-il7nt
    @ROHANMANNA-il7nt 4 месяца назад +2

    isn't its time complexity is O(n^2)?? and the constraint was of 5*10^4 then how is it possible to do it with O(n^2)??
    Pls anyone help
    🙏

    • @aymanrahman4118
      @aymanrahman4118 4 месяца назад

      It’s not in O(n^2). Your making a pass through one of the lists to build its hashset, then you iterate through the other list to check all possible prefixes for each num. Getting all possible prefixes of a number is considered constant.

  • @its_me_tabs
    @its_me_tabs 4 месяца назад

    13:23 I think they are right and you missed some math. I stand to be corrected butthe number of digits in a number is not given by its logarithm to base 10 but rather (1 + its logarithm to base 10). For example, 100 has 3 digits because its logarithm to base 10 is 2.

  • @xenitane
    @xenitane 4 месяца назад +3

    just make a trie with the numbers using digits and find the prefix lengths

    • @olamide203
      @olamide203 4 месяца назад

      Those were my initial thoughts as well

  • @MikPosp
    @MikPosp 4 месяца назад +1

    Thanks for your video!
    Here are a couple of oneliners:
    class Solution:
    def longestCommonPrefix(self, a: List[int], b: List[int]) -> int:
    return len(str(max((f:=lambda e:{v//10**i for v in e for i in range(9)})(a)&f(b)) or ''))
    class Solution:
    def longestCommonPrefix(self, a: List[int], b: List[int]) -> int:
    return max(map(len,and_(*({s[:i] for s in map(str,e) for i in range(len(s)+1)} for e in (a,b)))))

  • @satyamjha68
    @satyamjha68 4 месяца назад

    Solved it!

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 4 месяца назад +1

    I solved this on my own using Trie. 50% runtine efficient but only 5% memory efficiency. Happy that I solved it without any bug.

  • @miruomoo
    @miruomoo 3 месяца назад

    u the goat

  • @mirkelor
    @mirkelor 4 месяца назад

    We can also get max number if not 0 then convert it to string to get length in the end works too

  • @adarshnayak6803
    @adarshnayak6803 4 месяца назад

    👍👍

  • @GeneYeoYT
    @GeneYeoYT 4 месяца назад

    nice thumbnail

  • @MrAg1980
    @MrAg1980 4 месяца назад +2

    First

  • @nikhil199029
    @nikhil199029 3 месяца назад

    my ex has also become very large

  • @vbhv-gupta
    @vbhv-gupta 4 месяца назад +1

    optimization "n not in prefix_set" is actually redundant, since its hashset when we try to add an element which is already present in the hashset, this check will be executed internally.
    So, this doesn't actually optimize the runtime.

    • @_duckk
      @_duckk 4 месяца назад

      It will terminate the loop earlier, so we can avoid computing hash of the key more times than needed

    • @_duckk
      @_duckk 4 месяца назад

      Just benchmark the code and see the difference for yourself