Skip to main content

Find Anagram Mappings - Solution & Explanation

EasyPremiumFree on FleetCodeArrayHash Table6 min readAsked at: Google
Practice this problem

Problem Statement

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 <= 100
  • nums2.length == nums1.length
  • 0 <= nums1[i], nums2[i] <= 105
  • nums2 is an anagram of nums1.

Approach Overview

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.

Solution

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SearchO(n²)O(1)Useful for understanding the problem or when input size is very small
Hash Table MappingO(n)O(n)General case; optimal approach using constant-time lookups

Video Solution

Leetcode 760. Find Anagram Mappings • Algorithms Casts • 573 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Find Anagram Mappings easy or hard?
Find Anagram Mappings is classified as an Easy problem. The main concept is recognizing that repeated searches can be replaced with a hash map to achieve linear time.
Find Anagram Mappings Python/Java solution
The typical implementation builds a dictionary or HashMap from nums2 values to indices. Then a second loop over nums1 fills the result array using map lookups. The same logic works in Python, Java, C++, Go, and TypeScript with standard hash map structures.
How to solve Find Anagram Mappings in O(n)?
First build a hash map that stores the indices of every value in nums2. Then iterate through nums1 and look up the corresponding index for each value in the map. If duplicates exist, store lists of indices and pop one when used. Both passes together produce an O(n) solution.
What is the best approach for Find Anagram Mappings?
The best approach uses a hash table to map values in nums2 to their indices. After building the map in one pass, iterate through nums1 and retrieve each index using constant-time lookups. This reduces the complexity to O(n) time with O(n) extra space.
Is Find Anagram Mappings asked at Google/Amazon/Meta?
Find Anagram Mappings is an easy-level interview problem that tests hash table fundamentals and array indexing. Variations of index mapping and hash-based lookups appear frequently in interviews at companies like Amazon and Google.
What data structure is used in Find Anagram Mappings?
A hash table (hash map) is the key data structure. It stores value-to-index mappings from nums2 so that each lookup for nums1 can be done in constant time.
What is the time complexity of Find Anagram Mappings?
The optimal hash table solution runs in O(n) time because each array is traversed once and hash lookups take O(1) on average. The brute force alternative requires O(n²) time since it scans nums2 for every element in nums1.

Ready to solve this problem?

Practice Find Anagram Mappings with our built-in code editor and test cases.

Practice on FleetCode