Find Anagram Mappings

easy hash-map arrays

Problem

Given arrays A and B where B is an anagram of A, return an array mapping where mapping[i] is the index in B of A[i].

InputA=[12,28,46,32,50], B=[50,12,32,46,28]
Output[1,4,3,2,0]
Each A[i] is found at the listed index of B.

def anagramMappings(A, B):
    idx = {v: i for i, v in enumerate(B)}
    return [idx[v] for v in A]
function anagramMappings(A, B) {
  const idx = new Map();
  B.forEach((v, i) => idx.set(v, i));
  return A.map(v => idx.get(v));
}
int[] anagramMappings(int[] A, int[] B) {
    Map<Integer,Integer> idx = new HashMap<>();
    for (int i = 0; i < B.length; i++) idx.put(B[i], i);
    int[] res = new int[A.length];
    for (int i = 0; i < A.length; i++) res[i] = idx.get(A[i]);
    return res;
}
vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
    unordered_map<int,int> idx;
    for (int i = 0; i < (int)B.size(); i++) idx[B[i]] = i;
    vector<int> res;
    for (int v : A) res.push_back(idx[v]);
    return res;
}
Time: O(n) Space: O(n)