Skip to content

Instantly share code, notes, and snippets.

@digitalmio
Last active September 24, 2024 17:02
Show Gist options
  • Save digitalmio/1f107bfca5f56584af9bf40ba86acf31 to your computer and use it in GitHub Desktop.
Save digitalmio/1f107bfca5f56584af9bf40ba86acf31 to your computer and use it in GitHub Desktop.
const test = (strs: string[]) =>
Object.values(
strs.reduce((acc, el) => {
const key = el.split("").sort().join("");
acc[key] = [...(acc[key] || []), el].sort();
return acc;
}, {} as Record<string, string[]>)
).sort((a, b) => a.length - b.length);
// ========================
console.log(test(["eat", "tea", "tan", "ate", "nat", "bat"]));
console.log(test([""]));
console.log(test(["a"]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment