LeetCode Verifying an Alien Dictionary Solution Explained - Java

  Рет қаралды 25,092

Nick White

5 жыл бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-developer-academy
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nickwhite
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow Me on X/Twitter - x.com/nickwhitereal
Follow My Instagram - nickwwhite
Other Social Media
----------------------------------------------
Discord - discord.gg/ZGyc2nZyAx
Twitch - www.twitch.tv/nickwhitettv
TikTok - www.tiktok.com/@nickwhitetiktok
LinkedIn - www.linkedin.com/in/nicholas-w-white/
Show Support
------------------------------------------------------------------------------
Patreon - www.patreon.com/nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - kzbin.info/door/1fLEeYICmo3O9cUsqIi7HAjoin
#coding #programming #softwareengineering

Пікірлер: 29
@NikashKumar
@NikashKumar 4 жыл бұрын
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 4 жыл бұрын
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()
@JonathanDascenzo
@JonathanDascenzo 4 жыл бұрын
More readable? Sure. Slower? Definitely.
@mvishukumar2922
@mvishukumar2922 3 жыл бұрын
Creating the hashmap in the order that was provided as the argument. for(int i=0; i
@joydeeprony89
@joydeeprony89 2 жыл бұрын
no fancy stuffs and all, only pure logic and coding to solve a problem.
@benakin9172
@benakin9172 3 жыл бұрын
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
@ziyuwang4406
@ziyuwang4406 3 жыл бұрын
"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 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.
@conanlv8724
@conanlv8724 5 жыл бұрын
Quick question, why would ["hello", "hel"] not sorted v.s. ["hel", "hello"] is sorted ?
@sajidrahman7065
@sajidrahman7065 4 жыл бұрын
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 4 жыл бұрын
@@sajidrahman7065 Ah Ha, I see. Thank you very much :D
@harsh_hybrid_thenx
@harsh_hybrid_thenx 4 жыл бұрын
Nick has another error besides runtime error, compile error, TLE , Wrong answer etc. return 'wifi error'
@asifbilla4924
@asifbilla4924 4 жыл бұрын
Excellent explanation, Thanks Nick.
@blackgoldendoodle
@blackgoldendoodle 4 жыл бұрын
Pretty clear solution.
@koustubhmokashi4047
@koustubhmokashi4047 3 жыл бұрын
Was there any mosquito 🦟? Kidding.. nice explanation 😊
@amrholo4445
@amrholo4445 2 жыл бұрын
Thanks a lot
@deepgosalia318
@deepgosalia318 5 жыл бұрын
Hi, discord link is invalid
@sandipchanda6522
@sandipchanda6522 4 жыл бұрын
Thank you Nick
@AmanKumar-kz1rn
@AmanKumar-kz1rn 3 жыл бұрын
you just scares me why you clap lol
@ChessFlix
@ChessFlix 3 жыл бұрын
Yooo Nick, I found a nugget.
@algorithmimplementer415
@algorithmimplementer415 4 жыл бұрын
Awesome video!!
@saranyailla8762
@saranyailla8762 5 жыл бұрын
Thanks !
@NickWhite
@NickWhite 5 жыл бұрын
thank you!
@altearjen6838
@altearjen6838 5 жыл бұрын
very good!!
@oakes233
@oakes233 5 жыл бұрын
Thanks Nick,
@NickWhite
@NickWhite 5 жыл бұрын
thanks for watching dude
@peeyar2000
@peeyar2000 5 жыл бұрын
Thanks...
@senthelazy667
@senthelazy667 4 жыл бұрын
u r awesome
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 109 МЛН
Cool Parenting Gadget Against Mosquitos! 🦟👶
00:21
TheSoul Music Family
Рет қаралды 14 МЛН
Smart Sigma Kid #funny #sigma
00:14
CRAZY GREAPA
Рет қаралды 6 МЛН
规则,在门里生存,出来~死亡
00:33
落魄的王子
Рет қаралды 32 МЛН
Hardware tools repair tool high performance tool
0:16
Hardware tools
Рет қаралды 4,3 МЛН
The force of electromagnetic eddy currents
0:31
Nikola Toy
Рет қаралды 34 МЛН
How To Install a Gant Fan #satisfying @MNSmartTech
0:13
MN Tech Tips
Рет қаралды 3,5 МЛН
Кому новенький айфон
0:19
Новостной Гусь
Рет қаралды 3,9 МЛН