Skip to content

Instantly share code, notes, and snippets.

@Turskyi
Created April 17, 2022 22:56
Show Gist options
  • Save Turskyi/e1ed1f62c84e300168b646d4b0f93bb9 to your computer and use it in GitHub Desktop.
Save Turskyi/e1ed1f62c84e300168b646d4b0f93bb9 to your computer and use it in GitHub Desktop.
Group the anagrams together.
/*
Given a list of strings, return a list of strings containing all anagrams grouped together.
strs = ["car", "arc", "bee", "eeb", "tea"],
return
[
["car","arc"],
["tea"],
["bee","eeb"]
]
* */
fun groupAnagrams(strs: Array<String>): List<List<String>> {
val hashMap: Map<List<Char>, List<String>> = strs.groupBy { word: String -> word.toList().sorted() }
return hashMap.map { it.value }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment