Ingayum Alien ahh exciting problem, Verifying an Alien Dictionary with Hashing

MarudhuPandiyan
4 min readFeb 3, 2024

--

Brief Of what we are going to do

Introduction:

It’s an Easy tag problem but the core of the concept is very important for upcoming problems like an alien and comparing and string problems in the question of what they are stating given a sequence of WORDS written in an alien language, and order of the alphabet you just return TRUE if the given words are sorted lexicographically in this alien language.

Question Decoding:

  • They are given a set of words written in the alien language
  • with order of the alphabet
  • you just return true if the given words are sorted lexicographically in this alien language.

Visual Explanation:

The core concept of the problem what should do what shouldn’t do
The core concept of the problem is what should do and what shouldn’t do

How do we gonna approach the problem:

Example of the problem
  1. I hope the above image will help you to clarify what they are asking if not just wait I’ll explain.
  2. You may imagine some gibberish sequence in your mind along with imagining two words for find is sorted or not with what you imagined gibberish okay
  3. Then create Map() to store every single gibberish I hope you all know how you store the letters one question might arise why do we want to store the gibberish in the Map() without storing it how sure we make a comparison with the letters map will help you just get the key from the map along with their value so we can easily compared with the words that’s why
  4. Just simply create a loop until the sequence of length inside of the loop using set() the key is your letters and the value is Ith value done
  5. Then create one more loop for getting what you imagined words from the arrays as an argument to some other function for code clarity if don’t want you can move further along with a single function which means (wordsone, wordstwo, along with your mapped gibberish) total 3 arguments.
  6. Finally, we came into the core function after passing the argument and then started making comparisons and extracting every single word from the parameter words if the char is not matched in that case only we should make a comparison why because if the char is matched there is no need to make any comparison then getting the gibberish values corresponding to the char in you hand exact char of gibberish values so then start a compare if char1 < char2 move on to the next char if not just simply return false still if you have any confusion don’t worry when we jump into the code we can easily map with pseudo-code and real code so jump into the code.

Code Showcase:

var isAlienSorted = function (words, alienOrder) {
let compareWords = (words1, words2, alienOrder) => {
let n = Math.min(words1.length, words2.length);
// extract every char from the words with the help of loop
for (let i = 0; i < n; i++) {
let char1 = words1[i];
let char2 = words2[i];
// as I'm told you before if char not matched do the comparison or just ignore
if (char1 !== char2) {
return alienOrder.get(char1) < alienOrder.get(char2);
}
}
return words1.length <= words2.length;
};
// mapping the gibberish
let alienCharOrder = new Map();
for (let i = 0; i < alienOrder.length; i++) {
alienCharOrder.set(alienOrder[i], i);
}
// console.log(alienCharOrder);
// if the compare function return false just return false because there is
// something happend some words are not sorted so simple
for (let j = 0; j < words.length - 1; j++) {
if (!compareWords(words[j], words[j + 1], alienCharOrder)) {
return false;
}
}
return true;
};

const words = ["word", "world", "row"];
// in alien order we assign 0 to 25 according to this h assigned 0 and l assigned 1 checking which one letter mismatch so we got initially mismatch the string h and l that's why i given to example this h---0 < l ---1 so true
const order = "hlabcdefgijkmnopqrstuvwxyz";
console.log(isAlienSorted(words, order));

Vanga Conclusion:

This one is a small alien which means a trailer of what we are going to do next biggggg alien don’t worry just stick with me we can easily solve any problem okay fine when you are going to close the tab or phone screen please don’t forget to hit the follow button and applaud button as well I am awaits you response so please share your thoughts in the command section I’ll try to resolve what you are asked thanks for sticking with me so far bye bye see ya at next blog.

--

--

MarudhuPandiyan

As a skilled JavaScript full-stack developer, I'm dedicated to crafting clean, efficient code for impactful web solutions.