JS Interview - Anagrams - Question 15

preview_player
Показать описание
This episode talks about how to create a function that will compare two strings to see if they are anagrams of each other.

Рекомендации по теме
Комментарии
Автор

Great. My solution:10000 lines. Steve completes in 2. Beautiful.

savvyshah
Автор

Great video as always! Just to add that your solution now includes trivial anagrams (that is when you compare two words that are the same, for example "hello" = "hello"). I wanted to exclude trivial anagrams so I added one additional check:
const isAnagram = (str1,  str2) => {



}

demian
Автор

we can compare the lengths before to skip some unnecessary computation

jasbindarsingh
Автор

i thought of a solution while this was running, chekc length of both strings, if that passes loop thru one of the words, have two variables called sum, while in the loop get the ascii value of each char add it to the variable for both words and if the variables are the same then its an anagram

zo
Автор

Thanks for the examples. How would you do it if there's uppercase letters? I used map but I feel like there are better ways to do it.


let mySolution = function (str1, str2) {
if(str1.split('').map((value) => === str2.split('').map((value) =>
console.log(true);
} else {
console.log(false);
}
};

console.log(mySolution("hello", "jello")); // false
console.log(mySolution("hello", "OLLEH")); // true
console.log(mySolution("hello", "OlLeH")); // true

frank
Автор

How about the performance ? Isnt it better to store the alphabets as key and then compare so there are less loops ? I would like to know the big O of the above solution. Great series btw !! :)

imhererahul
Автор

function isAnagram(str1, str2) {
sorted1 =
sorted2 =

return (sorted1 === sorted2);
}

console.log(isAnagram("qsha", "qsHa"));




It returns false, it should return true?

ДимитърСтефанов-гб
Автор

lets imagine that you are in the real world and in an real interview! if you write this as solution, they will nicely show you the way out !
have you ever heard about big O of sqrt N? if you use your solution with a bigger input text file, it takes forever to finish the calculation

papirusein