Find Anagram Mappings - Problem
Find Anagram Mappings

You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. This means that nums2 contains exactly the same elements as nums1, just in a different order.

Your task is to return an index mapping array mapping where mapping[i] = j means the i-th element in nums1 appears at index j in nums2.

Important notes:
• Both arrays may contain duplicates
• If there are multiple valid answers, return any one of them
• The arrays are guaranteed to be anagrams of each other

Example: If nums1 = [12, 28, 46, 32, 50] and nums2 = [50, 12, 32, 46, 28], then one valid mapping would be [1, 4, 3, 2, 0] because:
nums1[0] = 12 appears at nums2[1]
nums1[1] = 28 appears at nums2[4]
nums1[2] = 46 appears at nums2[3]
• And so on...

Input & Output

example_1.py — Basic Case
$ Input: nums1 = [12, 28, 46, 32, 50], nums2 = [50, 12, 32, 46, 28]
Output: [1, 4, 3, 2, 0]
💡 Note: nums1[0]=12 is found at nums2[1], nums1[1]=28 is found at nums2[4], nums1[2]=46 is found at nums2[3], nums1[3]=32 is found at nums2[2], nums1[4]=50 is found at nums2[0]
example_2.py — With Duplicates
$ Input: nums1 = [84, 46, 84], nums2 = [84, 46, 84]
Output: [2, 1, 0] or [0, 1, 2]
💡 Note: Since there are duplicate 84s, multiple valid answers exist. One possibility: first 84 maps to index 2, 46 maps to index 1, second 84 maps to index 0
example_3.py — Single Element
$ Input: nums1 = [1], nums2 = [1]
Output: [0]
💡 Note: The only element 1 in nums1 is found at index 0 in nums2

Constraints

  • 1 ≤ nums1.length ≤ 100
  • nums2.length == nums1.length
  • 0 ≤ nums1[i], nums2[i] ≤ 105
  • nums2 is an anagram of nums1

Visualization

Tap to expand
📚 Library Catalog System Analogy📋 Patron Requests (nums1)Books people want to find:Book ABook BBook CBook DBook ENeed to find shelf location for each book📚 Library Shelves (nums2)Books on shelves in random order:Book EShelf 0Book AShelf 1Book DShelf 2Book CShelf 3Book BShelf 4🗂️ Card Catalog (Hash Map)Instant lookup: Book Name → Shelf NumberBook A → Shelf 1Book B → Shelf 4Book C → Shelf 3Book D → Shelf 2Book E → Shelf 0⚡ O(1) lookup timevs O(n) shelf scanningBuild catalogfrom shelves🎯 Result: [1, 4, 3, 2, 0] - Shelf locations for requested books!
Understanding the Visualization
1
Catalog Creation
Create a catalog (hash map) of where each item is stored
2
Quick Lookups
For each requested item, instantly find its location using the catalog
3
Handle Duplicates
Use a stack system to manage multiple copies of the same item
4
Complete Mapping
All locations found efficiently in linear time
Key Takeaway
🎯 Key Insight: Hash maps provide O(1) average lookup time, transforming an O(n²) search problem into an O(n) mapping problem. Like a well-organized library catalog, we can instantly find any book's location!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 10
28.4K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen