Funcia
Lv.2hashsortingstring

Anagram Groups

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Anagram Groups

Given an array of lowercase words, group the words that are anagrams of each other and return the groups. Each group must be sorted in ascending lexicographic order (duplicates kept); the order of the groups does not matter.

Constraints

  • 1≤1 \le words.length ≤100,000\le 100{,}000, 1≤1 \le words[i].length ≤26\le 26, lowercase a–z only.

Example

["eat", "tea", "tan", "ate", "nat", "bat"] → [["ate", "eat", "tea"], ["nat", "tan"], ["bat"]] (any group order)

Expected time complexity: O(N⋅Llog⁡L)O(N \cdot L \log L). Scanning all existing groups for every word, O(N2)O(N^2), exceeds the performance time limit.

Function signature

solution(words: string[]) → string[][]

Expected complexity

Time
O(N * L log L)
Space
O(N * L)

Examples

wordsExpected
["eat","tea","tan","ate","nat","bat"][["ate","eat","tea"],["nat","tan"],["bat"]]
["a"][["a"]]

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 252130 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample02inputs and outputs
correctness408verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.