10760. Find Anagram Mappings

You are given two arrays arr1 and arr2, where arr2 is a rearrangement of the elements in arr1. In other words, arr2 is formed by shuffling the elements of arr1.

Your task is to determine an index mapping map such that map[i] = j indicates the element at position i in arr1 can be found at index j in arr2.

Both arrays may contain repeated elements.
If multiple valid mappings exist, return the lexicographically smallest valid array i.e. the one with the smaller element at the first differing index. .

For instance, consider the following example:

arr1 = [10, 25, 40, 30, 55]
arr2 = [55, 10, 30, 40, 25]

A valid mapping would be:

[1, 4, 3, 2, 0]

Explanation:

  • map[0] = 1 because arr1[0] = 10 is located at arr2[1].
  • map[1] = 4 because arr1[1] = 25 is located at arr2[4].
  • map[2] = 3 because arr1[2] = 40 is located at arr2[3].
  • map[3] = 2 because arr1[3] = 30 is located at arr2[2].
  • map[4] = 0 because arr1[4] = 55 is located at arr2[0].

Examples

Input: arr1 = [5, 15, 20, 10], arr2 = [10, 5, 20, 15]
Output: [1, 3, 2, 0]

Input: arr1 = [7, 7, 8], arr2 = [8, 7, 7]
Output: [2, 1, 0]

Input: arr1 = [3], arr2 = [3]
Output: [0]

Constraints

  • The length of arr1 and arr2 is the same and falls within 1 to 120.
  • Each element in arr1 and arr2 is an integer between 0 and 120000.




Please use Laptop/Desktop or any other large screen to add/edit code.