LeetCode Verifying an Alien Dictionary Solution Explained - Java

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • 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
  • Věda a technologie

Komentáře • 29

  • @NikashKumar
    @NikashKumar Před 4 lety +26

    haha, i see what you did there,, you corrected the error in the background during your wifi issue,, word2 was typed as word1.. good one.

  • @darod6098
    @darod6098 Před 4 lety +8

    Even that we know that we can use an array like Nick did (with .charAt(i) - 'a') to set/get a position, i think that it is far more readable with a hashmap:
    public int[] mapEnglishToAlien;
    Map alienDict = new HashMap();
    public boolean isAlienSorted(String[] words, String order) {
    for (int i = 0; i < order.length(); i++) {
    alienDict.put(order.charAt(i), i);
    }
    for (int i = 1; i < words.length; i++) {
    if(!compare(words[i-1], words[i])) {
    return false; //it is not sorted
    }
    }
    return true;
    }
    public boolean compare(String word1, String word2) {
    int i = 0;
    int j = 0;
    int positionAlienDictW1 = -1;
    int positionAlienDictW2 = -1;
    while (i < word1.length() && j < word2.length()) {
    positionAlienDictW1 = alienDict.get(word1.charAt(i));
    positionAlienDictW2 = alienDict.get(word2.charAt(j));
    if (positionAlienDictW1 < positionAlienDictW2) {
    return true;
    } else if (positionAlienDictW1 > positionAlienDictW2) {
    return false;
    }
    i++;
    j++;
    }
    if (positionAlienDictW1 == positionAlienDictW2) {
    //same words, check => ["hello", "hel"] is false and ["hel", "hello"] is true
    return word1.length()

  • @asifbilla4924
    @asifbilla4924 Před 4 lety

    Excellent explanation, Thanks Nick.

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

    Creating the hashmap in the order that was provided as the argument.
    for(int i=0; i

  • @joydeeprony89
    @joydeeprony89 Před 2 lety

    no fancy stuffs and all, only pure logic and coding to solve a problem.

  • @ziyuwang4406
    @ziyuwang4406 Před 3 lety

    "Oh, 'h' is 7, 'h' is 7!" LOL Thanks Nick. For the first time I feel like I'm watching a leetcode video as a comedy show.

  • @ankitagarwal4914
    @ankitagarwal4914 Před 4 lety +4

    Truely, This is the cleanest code I have seen on this problem. 1k likes for compare function. Code is readable , separation of concerns, really well explained.Big fan of yours.

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

    be careful guys, line 24, the second part of word2 was changed to word2...not word1 ....at the end of the video. thats how he got the answer

  • @sandipchanda6522
    @sandipchanda6522 Před 4 lety

    Thank you Nick

  • @blackgoldendoodle
    @blackgoldendoodle Před 4 lety

    Pretty clear solution.

  • @algorithmimplementer415

    Awesome video!!

  • @saranyailla8762
    @saranyailla8762 Před 4 lety +1

    Thanks !

  • @altearjen6838
    @altearjen6838 Před 4 lety

    very good!!

  • @amrholo4445
    @amrholo4445 Před 2 lety

    Thanks a lot

  • @deepgosalia318
    @deepgosalia318 Před 4 lety

    Hi, discord link is invalid

  • @koustubhmokashi4047
    @koustubhmokashi4047 Před 3 lety

    Was there any mosquito 🦟? Kidding.. nice explanation 😊

  • @harsh_hybrid_thenx
    @harsh_hybrid_thenx Před 4 lety +1

    Nick has another error besides runtime error, compile error, TLE , Wrong answer etc. return 'wifi error'

  • @conanlv8724
    @conanlv8724 Před 4 lety

    Quick question, why would ["hello", "hel"] not sorted v.s. ["hel", "hello"] is sorted ?

    • @sajidrahman7065
      @sajidrahman7065 Před 4 lety +7

      In most common lexicographical ordering (i.e. dictionary ordering), shorter words which are overlapped with prefixes of longer words appear before. Hence, ["hello", "hel"] is not sorted but. ["hel", "hello"] is sorted.

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

      @@sajidrahman7065 Ah Ha, I see. Thank you very much :D

  • @peeyar2000
    @peeyar2000 Před 4 lety

    Thanks...

  • @oakes233
    @oakes233 Před 4 lety +1

    Thanks Nick,

  • @senthelazy667
    @senthelazy667 Před 4 lety

    u r awesome

  • @ChessFlix
    @ChessFlix Před 3 lety

    Yooo Nick, I found a nugget.

  • @AmanKumar-kz1rn
    @AmanKumar-kz1rn Před 3 lety +1

    you just scares me why you clap lol