Created
April 17, 2022 22:56
-
-
Save Turskyi/e1ed1f62c84e300168b646d4b0f93bb9 to your computer and use it in GitHub Desktop.
Group the anagrams together.
This file contains 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
/* | |
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