Skip to content

Instantly share code, notes, and snippets.

@nikonov91-dev
Created April 5, 2022 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikonov91-dev/ad4031816324cb0eebf9c2ab54e5325e to your computer and use it in GitHub Desktop.
Save nikonov91-dev/ad4031816324cb0eebf9c2ab54e5325e to your computer and use it in GitHub Desktop.
function funWithAnagrams(text) {
let arr = [text[0]];
const areTheSameAnagram = (a,b) =>
a.split('').sort().join('') === b.split('').sort().join('');
text.forEach( e =>
!arr.find(ee => areTheSameAnagram(e, ee)) && arr.push(e)
)
return arr.sort()
}
Two strings are anagrams if they are permutations of each other. In other words, both strings have the same size and the same characters. For example, "aaagmnrs" is an anagram of "anagrams". Given an array of strings, remove each string that is an anagram of an earlier string, then return the remaining array in sorted order.
str = ['code', 'doce', 'ecod', 'framer', 'frame']
STDIN Function
----- --------
4 → n = 4
code → text = ["code","aaagmnrs","anagrams","doce"]
aaagmnrs
anagrams
doce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment