I just wanted to say your videos are PURE GOLD. From a students perspective, these step by step walk throughs are super helpful and you explain it so much better than teachers/professors!
Hot takes also include assigning the first 26 primes uniquely to each Letter, mapping the Words to the corresponding primes and comparing each accumulated products
Simply sort the characters in a string, a to z, and join to create a new word. If the word is the same for both strings it's an anagram. Much simpler to understand.
Definitely a simpler approach but in terms of time complexity, sorting the characters of both strings and joining will be greater than if we used a frequency map to solve the problem.
My Solution /** * @param {string} s * @param {string} t * @return {boolean} */ var isAnagram = function(s, t) { if (s.length !== t.length) { return false; } let frequency = {}; // Count frequency of each character in `s` for (let element of s) { frequency[element] = (frequency[element] || 0) + 1; } // Decrement frequency count for each character in `t` for (let element of t) { if (!frequency[element]) { return false; } frequency[element]--; } // If all values in frequency are zero, the strings are anagrams for (let count in frequency) { if (frequency[count] !== 0) { return false; } } return true; };
Dude this was kicking my ass recently. Your way of explaining made it click... Thank you.
Awesome! Glad it helped!
I just wanted to say your videos are PURE GOLD. From a students perspective, these step by step walk throughs are super helpful and you explain it so much better than teachers/professors!
Thank you!
This was so clear to understand, thanks!
Hot takes also include assigning the first 26 primes uniquely to each Letter, mapping the Words to the corresponding primes and comparing each accumulated products
Thank you!
very helpful
Simply sort the characters in a string, a to z, and join to create a new word. If the word is the same for both strings it's an anagram. Much simpler to understand.
Definitely a simpler approach but in terms of time complexity, sorting the characters of both strings and joining will be greater than if we used a frequency map to solve the problem.
Thank you
Goated
Your short video was so clear than this one, helpful content nevertheless.
Will be improving production and explanation in new vids!
My Solution
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if (s.length !== t.length) {
return false;
}
let frequency = {};
// Count frequency of each character in `s`
for (let element of s) {
frequency[element] = (frequency[element] || 0) + 1;
}
// Decrement frequency count for each character in `t`
for (let element of t) {
if (!frequency[element]) {
return false;
}
frequency[element]--;
}
// If all values in frequency are zero, the strings are anagrams
for (let count in frequency) {
if (frequency[count] !== 0) {
return false;
}
}
return true;
};