Published
Edited
Nov 30, 2020
Insert cell
md`# Check if two strings are anagrams`
Insert cell
function isAnagram(word1, word2) {
if (word1.length !== word2.length) return false;

// word2 = [...word2];
// for (let i = 0; i < word1.length; i++) {
// if (!word2.includes(word1[i])) return false;
// let index = word2.indexOf(word1[i]);
// word2.splice(index, 1);
// }

// return !word2.length;

return [...word1].sort().join('') === [...word2].sort().join('');
}
Insert cell
isAnagram('star', 'rats')
Insert cell
isAnagram('star', 'stars')
Insert cell
isAnagram('star', 'strr')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more