LeetCode Verifying an Alien Dictionary Solution Explained - Java
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
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.
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()
More readable? Sure. Slower? Definitely.
Excellent explanation, Thanks Nick.
Creating the hashmap in the order that was provided as the argument.
for(int i=0; i
no fancy stuffs and all, only pure logic and coding to solve a problem.
"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.
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.
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
Thank you Nick
Pretty clear solution.
Awesome video!!
Thanks !
thank you!
very good!!
Thanks a lot
Hi, discord link is invalid
Was there any mosquito 🦟? Kidding.. nice explanation 😊
Nick has another error besides runtime error, compile error, TLE , Wrong answer etc. return 'wifi error'
Quick question, why would ["hello", "hel"] not sorted v.s. ["hel", "hello"] is sorted ?
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.
@@sajidrahman7065 Ah Ha, I see. Thank you very much :D
Thanks...
Thanks Nick,
thanks for watching dude
u r awesome
Yooo Nick, I found a nugget.
you just scares me why you clap lol