531. Find Anagram Mappings

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 i-th 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.

After returning array, our driver code will validate output and prints "true" if output is correct else will show output and expected one.

Example 1:

Input: nums1 = [20, 40, 60, 80, 100], nums2 = [100, 20, 80, 60, 40]

Output: [1, 4, 3, 2, 0]

Explanation:

  • mapping[0] = 1 because the 0-th element of nums1 appears at nums2[1].
  • mapping[1] = 4 because the 1-st element of nums1 appears at nums2[4], and so on.

Example 2:

Input: nums1 = [75, 90], nums2 = [75, 90]

Output: [0, 1]

Explanation:

  • mapping[0] = 0 because 75 appears at nums2[0].
  • mapping[1] = 1 because 90 appears at nums2[1].

Now Your Turn!

Pick the correct output for the given input

Input: nums1 = [1, 2, 3, 4, 5], nums2 = [3, 1, 4, 5, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> anagramMappings(vector<int>& nums1, vector<int>& nums2) {
// Your code goes here
}
};
 
Test Case

Input:

Nums1
Nums2