Example 1 · empty
- Arguments
- words
[]
- Expected return
[]
Test data
- Input
{"words":[]}- Expected output
[]
Put words with the same letter counts into the same group.
func GroupAnagrams(words []string) [][]stringExamples use {"words": [...]}.
The runner calls your functions directly; you do not need to parse JSON.
[]
[]
{"words":[]}[]
[ "tea", "tan", "eat", "ate", "nat", "bat" ]
[
[
"tea",
"eat",
"ate"
],
[
"tan",
"nat"
],
[
"bat"
]
]{"words":["tea","tan","eat","ate","nat","bat"]}[["tea","eat","ate"],["tan","nat"],["bat"]]
[ "ab", "aab", "ba", "aba" ]
[
[
"ab",
"ba"
],
[
"aab",
"aba"
]
]{"words":["ab","aab","ba","aba"]}[["ab","ba"],["aab","aba"]]
[ "", "a", "", "a" ]
[
[
"",
""
],
[
"a",
"a"
]
]{"words":["","a","","a"]}[["",""],["a","a"]]
Two words are anagrams exactly when each letter has the same frequency, so equal signatures identify precisely the intended groups. Creating groups on first encounter orders them by their first input member, and appending during the same scan preserves order within each group.
O(C + W) time for C total characters and W words with the fixed 26-letter alphabet. Auxiliary signature/index storage is O(G), counting fixed-size signatures, for G groups; returned group lists hold O(W) word references.
Checking execution availability…
View inputs and expected outputs. Examples do not run your code.