Verifying an Alien Dictionary - Leetcode 953 - Python

Sdílet
Vložit

Komentáře • 39

  • @NeetCode
    @NeetCode  Před 3 lety +4

    Alien Dictionary (HARD) - czcams.com/video/6kTZYvNNyps/video.html

  • @MIDNightPT4
    @MIDNightPT4 Před 2 lety +4

    Awesome solution!! Makes it much more clean the way you stored w1,w2 as a variable

  • @sproutboot
    @sproutboot Před 2 lety +2

    I had a hard time understand the question, thank you. for the first 2~3min I had a good understanding of the problem

  • @danielsun716
    @danielsun716 Před rokem

    a little tricky thing is this solution does not use minLen of w1 and w2. But that is an excellent trick to solve the pre-work.

  • @jonaskhanwald566
    @jonaskhanwald566 Před 3 lety +1

    Please do some videos on pattern matching (KMP algorithm)

  • @sirpavr3182
    @sirpavr3182 Před 3 lety +15

    would be great if you could discuss the complexity on your videos.

  • @director8656
    @director8656 Před 3 lety +2

    Great Video as usual, thank you!

  • @amankassahunwassie587
    @amankassahunwassie587 Před rokem +6

    How this is a graph problem?

    • @OfficialZushi
      @OfficialZushi Před 2 měsíci

      My guess is that you can make a prefix Tree by graphing each word. Then the validation algorithm would be traversing the tree.
      Might be better when there's a large amount of words, but idrk

  • @aryamankukal1056
    @aryamankukal1056 Před 2 lety +4

    8:58 why don't you have to continue when you found the first differing char?

    • @asdasddas100
      @asdasddas100 Před 2 lety +9

      Because if I give you two strings:
      "Apple"
      "Banana"
      You only need to compare the first character to know that Apple comes first. You only need to continue if the first character is the same and continue character by character

  • @MP-ny3ep
    @MP-ny3ep Před rokem

    Thank you , awesome explanation.

  • @anupamkolay193
    @anupamkolay193 Před rokem

    Best Explanation possible.

  • @amandatao9622
    @amandatao9622 Před 2 lety +3

    Thanks!

  • @JJ-qv8co
    @JJ-qv8co Před 3 lety

    is there anything better than O(n2) ?

  • @asadbekshomurodov8416

    Bro this solution just perfect !!!

  • @abhinavgupta7048
    @abhinavgupta7048 Před 7 měsíci

    Its complexity is O(N*26), am I right?

  • @yang5843
    @yang5843 Před rokem

    This code is so well written

  • @the-tech-jerry
    @the-tech-jerry Před 8 měsíci

    I still wonder why this question is in graph section of your road map.

  • @krateskim4169
    @krateskim4169 Před rokem

    Awesome explanation

  • @xijinping5064
    @xijinping5064 Před 9 měsíci

    I did this in Go, turns out not using a hashmap is faster because the order string is just 26 bytes lol

  • @kirillzlobin7135
    @kirillzlobin7135 Před 26 dny

    Amazing solution

  • @amrholo4445
    @amrholo4445 Před 2 lety

    Thanks a lot sir

  • @expertium2255
    @expertium2255 Před rokem +1

    After checking solutions on leetcode I found a much shorter and much more readable one:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
    return words == sorted(words, key=lambda word:[order.index(c) for c in word])
    Though people in the comments say that the interviewer would not allow this (I don't know why, I'm a beginner btw).

  • @edwardteach2
    @edwardteach2 Před 2 lety +2

    U an Alien God

  • @aa10259
    @aa10259 Před rokem

    What's the use of the "break" ?

    • @trueknowledge1981
      @trueknowledge1981 Před rokem

      if it hits that case where your chars are not equal and by alien dict it comes first which falsy, we should not any of further chars,
      to stop there we use break statement

    • @KrishnaSingh-yc7wp
      @KrishnaSingh-yc7wp Před rokem

      @@trueknowledge1981 didn't got u

    • @barissimsek
      @barissimsek Před 5 měsíci

      When you find the first different char you don't need to go further. Because that different character (the most significant different) will define the order in the dictionary. Like Ape and Apple. 3rd chars are 'e' and 'p'. This information will define the order. Rest of the chars doesn't matter. Hence break.

  • @maamounhajnajeeb209
    @maamounhajnajeeb209 Před rokem

    thanks

  • @MiravMehtaishere
    @MiravMehtaishere Před 3 lety +1

    Is the time complexity , O(N-sqaured) ?

  • @sk_4142
    @sk_4142 Před 10 měsíci

    bro, i think you put this problem in the wrong category on your website. don't think it belongs in the graphs section!

  • @superpupernone
    @superpupernone Před 3 měsíci

    I didn't get it: why this problem in Graphs?!

  • @xjlonelystar
    @xjlonelystar Před 2 lety

    can't even solve an ez question -- cry in silence --

  • @mageshyt2550
    @mageshyt2550 Před rokem

    class Solution:
    def isAlienSorted(self, words, order: str) -> bool:
    order_list = {order[i]: i for i in range(len(order))}
    def helper(word1, word2):
    # check if word1 is lexicographically smaller than word2
    for i in range(min(len(word1), len(word2))):
    if order_list[word1[i]] < order_list[word2[i]]:
    return True
    elif order_list[word1[i]] > order_list[word2[i]]:
    return False
    # if same prefix, check length
    return True if len(word1) < len(word2) else False
    for i in range(len(words)-1):
    # if same word , continue
    if words[i] == words[i+1]:
    continue
    if not helper(words[i], words[i+1]):
    return False
    return True

  • @james3414
    @james3414 Před 2 lety +3

    Thanks!