Check if two collections of strings are anagrams
Filed under Wells Fargo
Free read 2 of 3Confirm if collections mean concatenated single strings or multiset comparison of all elements combined.
The question
Given two collections of strings, write code to check whether they are anagrams of each other (i.e., contain the exact same characters with the same frequencies, considering either all strings concatenated or element-wise).
Constraints
- Collections can contain strings of varying lengths.
- Character set is typically lowercase English alphabets.
Examples
- Input
- list1 = ["listen", "silent"], list2 = ["enlist", "tinsil"]
- Output
- True
- Why
- Concatenating or matching the character frequencies across the collections yields identical counts.
Approach
Confirm if collections mean concatenated single strings or multiset comparison of all elements combined.
Sort the combined characters of both collections and compare the resulting sorted arrays/strings.
Use a hash map or frequency array of size 26 to tally characters from the first collection, then decrement using the second collection.
Different total lengths between the two collections should immediately return false.
Solution
O(N + M) time where N and M are total characters, O(1) space for fixed alphabet
from collections import Counter
class Solution:
def checkAnagramCollections(self, list1: list[str], list2: list[str]) -> bool:
c1, c2 = Counter(), Counter()
for s in list1: c1.update(s)
for s in list2: c2.update(s)
return c1 == c2
Tips
- What they are testing: Test empty collections, mismatched total character counts, and case sensitivity.
- Common mistake: Assuming individual string mapping instead of aggregating all characters across the collections.
Related Wells Fargo questions
Collected from public candidate posts and coding forums. Updated Nov 2026.