Created
October 4, 2023 19:18
-
-
Save scarletquasar/4c7dc9e64976db6395d6731346b8c0cc to your computer and use it in GitHub Desktop.
sorted words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const words = ["cat", "act", "tac", "art", "tar"]; | |
const expectedOutput = { | |
"act": ["cat", "act", "tac"], | |
"atr": ["art", "tar"], | |
} | |
const sortedWords = words | |
.map(word => word.split('')) | |
.map(array => array.sort()) | |
.map(array => array.join('')); | |
const groups: Record<string, string[]> = {}; | |
for (const word of words) { | |
groups[word.split('').sort().join('')] = []; | |
} | |
sortedWords.forEach((word, index) => { | |
groups[word].push(words[index]); | |
}) | |
console.log(groups) | |
console.log(expectedOutput) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment