You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. Both arrays may contain duplicates.
Return an index mapping array mapping from nums1 to nums2 where mapping[i] = j means the ith element in nums1 appears in nums2 at index j. If there are multiple answers, return any of them.
An array a is an anagram of an array b means b is made by randomizing the order of the elements in a.
Example 1:
Input: nums1 = [12,28,46,32,50], nums2 = [50,12,32,46,28] Output: [1,4,3,2,0] Explanation: As mapping[0] = 1 because the 0th element of nums1 appears at nums2[1], and mapping[1] = 4 because the 1st element of nums1 appears at nums2[4], and so on.
Example 2:
Input: nums1 = [84,46], nums2 = [84,46] Output: [0,1]
Constraints:
1 <= nums1.length <= 100nums2.length == nums1.length0 <= nums1[i], nums2[i] <= 105nums2 is an anagram of nums1.Problem Overview: You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. The task is to return an array mapping such that mapping[i] is the index j where nums1[i] == nums2[j]. Since the arrays are anagrams, every value in nums1 appears somewhere in nums2.
Approach 1: Brute Force Search (O(n²) time, O(1) space)
The most direct method is to scan nums2 for every element in nums1. For each index i in nums1, iterate through nums2 until you find a matching value and store that index in the result array. This works because the problem guarantees both arrays contain the same multiset of values. The drawback is the nested iteration: each lookup in nums2 may take up to O(n), resulting in overall O(n²) time. Space usage stays O(1) aside from the output array. This approach is acceptable for small inputs but does not scale well.
Approach 2: Hash Table Mapping (O(n) time, O(n) space)
A faster solution uses a hash map to record where each value appears in nums2. First iterate through nums2 and store value → index in a hash table. Then iterate through nums1 and perform constant‑time lookups to retrieve the corresponding index from the map. Each lookup takes O(1) on average, so the entire algorithm runs in O(n) time with O(n) additional space for the map.
Handling duplicates requires a small adjustment. Instead of storing a single index, store a list (or stack) of indices for each value in nums2. When you encounter that value in nums1, pop one index from the list and place it into the result. This guarantees every occurrence is mapped correctly while preserving O(n) complexity.
This solution relies on a hash table for constant‑time lookups, a common pattern in many hash table problems. The iteration over the arrays is straightforward and uses basic array traversal. The key insight is that searching repeatedly is unnecessary when you can precompute the positions once.
Recommended for interviews: The hash table approach is the expected solution. Interviewers want to see that you recognize the repeated lookup pattern and replace it with constant‑time hashing. Mentioning the brute force solution first demonstrates understanding of the problem, while transitioning to the O(n) hash map optimization shows strong problem‑solving instincts.
We use a hash table d to store each element of the array nums2 and its corresponding index. Then we iterate through the array nums1, and for each element nums1[i], we retrieve its corresponding index from the hash table d and store it in the result array.
The time complexity is O(n) and the space complexity is O(n), where n is the length of the array.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Search | O(n²) | O(1) | Useful for understanding the problem or when input size is very small |
| Hash Table Mapping | O(n) | O(n) | General case; optimal approach using constant-time lookups |
Leetcode 760. Find Anagram Mappings • Algorithms Casts • 570 views views
Watch 7 more video solutions →Practice Find Anagram Mappings with our built-in code editor and test cases.
Practice on FleetCode