A-004● Day 042026.05.07mediumleetcode #49neetcode150
Group Anagrams
#array#hash-table#string#sorting
01
Problem
· problemGiven an array of strings strs, group the anagrams together. You can return the answer in any order.
constraints
- · 1 ≤ strs.length ≤ 10^4
- · 0 ≤ strs[i].length ≤ 100
- · strs[i] consists of lowercase English letters
// paraphrased summary — see source for full text
examples
02
Pre-solve
· pre-solve● 1list shown→● 2select→● 3reveal
- ☐What makes two strings anagrams of each other?
- ☐How can we create a unique signature that identifies all anagrams of a string?
- ☐Which data structure allows us to group strings by a shared signature efficiently?
- ☐Does the order of groups in the output matter?
- ☐Is it necessary to sort the input array before processing?
- ☐Must each group's strings be sorted before returning?
- ☐Can we use sorted strings as keys instead of character frequencies?
check the items you would ask, then press confirm
// session-only state — refresh resets (repeatable practice)
03
Logic Structure
· logic● 1slots shown→● 2pick per slot→● 3reveal
// pick one code line per slot to assemble the algorithm flow. no typing — just the logic skeleton.
step 1· Initialize result dictionary
○
groups = {}○
groups = collections.defaultdict(list)
○
groups = []
step 2· Iterate through each string
○
for s in strs: # O(m)
○
for i in range(len(strs)): s = strs[i]
○
for s in strs[::-1]:
step 3· Count character frequencies│ nested
○
count[char] = count.get(char, 0) + 1
○
count[char] += 1
○
count[char] = 1
step 4· Create canonical key from counts│ nested
○
tup = tuple(sorted(count.items())) # O(1) because there is limited amount of possible keys in the alphabet -> O(26) + O(26*log26) + O(26)
○
tup = sorted(count.items())
○
tup = tuple(count.keys())
step 5· Add string to group│ nested
○
groups[tup].append(s)
○
groups[tup] = [s]
○
groups.append((tup, s))
step 6· Return grouped results
○
return list(groups.values())
○
return groups
○
return list(groups.keys())
pick one option per slot
// format: slot — recursive / DP patterns use ordering / state-first formats. ADR-08 follow-up.
04
Solve · Trace
· solvemental dry-run cases
// walk each case in your head; expand the worked example below if stuck.
case 1
["eat","tea","tan","ate","nat","bat"]→
[["eat","tea","ate"],["tan","nat"],["bat"]]
case 2
[""]→
[[""]]
case 3
["a"]→
[["a"]]
// UI does not walk-through — you do the dry-run mentally. Expand the worked example if stuck.