KatInterview

762. Find Anagram Mappings

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find an index mapping from an array nums1 to its anagram nums2. Specifically, for each element nums1[i], we need to find an index j such that nums1[i] == nums2[j]. We need to return an array mapping where mapping[i] = j.

Solution

Find Anagram Mappings in Python

class Solution:
    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # Create a dictionary to store the index of each element in nums2.
        # The key is the number and the value is its index.
        # If there are duplicates in nums2, this will store the last seen index.
        num_to_index = {val: i for i, val in enumerate(nums2)}
        
        # Create the result list using a list comprehension.
        # For each number in nums1, look up its index from the dictionary.
        # The problem guarantees that every number in nums1 is in num_to_index.
        return [num_to_index[num] for num in nums1]

Find Anagram Mappings in Java

class Solution {
    public int[] anagramMappings(int[] nums1, int[] nums2) {
        // Create a hash map to store the index of each element in nums2.
        // The key is the number and the value is its index.
        Map<Integer, Integer> numToIndex = new HashMap<>();
        for (int i = 0; i < nums2.length; i++) {
            numToIndex.put(nums2[i], i);
        }
        
        // Create the result array.
        int[] mapping = new int[nums1.length];
        
        // For each element in nums1, find its index from the map and
        // populate the result array.
        for (int i = 0; i < nums1.length; i++) {
            mapping[i] = numToIndex.get(nums1[i]);
        }
        
        return mapping;
    }
}

Find Anagram Mappings in C++

class Solution {
public:
    vector<int> anagramMappings(vector<int>& nums1, vector<int>& nums2) {
        // Create a hash map to store the index of each element in nums2.
        // The key is the number and the value is its index.
        unordered_map<int, int> num_to_index;
        for (int i = 0; i < nums2.size(); ++i) {
            num_to_index[nums2[i]] = i;
        }
        
        // Create the result vector.
        vector<int> mapping(nums1.size());
        
        // For each element in nums1, find its index from the map and
        // populate the result vector.
        for (int i = 0; i < nums1.size(); ++i) {
            mapping[i] = num_to_index[nums1[i]];
        }
        
        return mapping;
    }
};

Find Anagram Mappings in TypeScript

function anagramMappings(nums1: number[], nums2: number[]): number[] {
    // Create a Map to store the index of each element in nums2.
    // The key is the number and the value is its index.
    const numToIndex = new Map<number, number>();
    for (let i = 0; i < nums2.length; i++) {
        numToIndex.set(nums2[i], i);
    }
    
    // For each element in nums1, find its index from the map.
    // The problem guarantees that every num from nums1 exists in numToIndex,
    // so the non-null assertion (!) is safe.
    return nums1.map(num => numToIndex.get(num)!);
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.