Given lists A
B
and, and is a anagram of. is an B
anagram of means are made by A
B
A
B
Rand Omizing the order of the elements in A
.
We want to find a index mapping P
, from A
to B
. A mapping P[i] = j
means the i
th element in A
appears with at B
index j
.
These lists and may A
B
contain duplicates. If There is multiple answers, output any of them.
For example, given
A = [50, 12, 32, 46, 28], 50]b = [+]
We should return
[1, 4, 3, 2, 0]
As
P[0] = 1
Because the
0
th element of
A
Appears at
B[1]
, and
P[1] = 4
Because the
1
St element of
A
Appears at
B[4]
, and so on.
Note:
A, B
has equal lengths in range [1, 100]
.
A[i], B[i]
is integers in range [0, 10^5]
.
Given the list of two A and B, B is an alphabetic group of a. B is an alphabetic group of a, which means that B is made by randomization of the order of elements in a.
We want to find an index map from a to B p. map p [i] = J means that the I element in a appears in B with index J.
These lists A and B may contain duplicates. If there are multiple answers, either of them is output.
/**
* @param {number[]} A
* @param {number[]} B
* @return {number[]}
*/
var anagramMappings = function (A, B) {
let res = [];
res.length = A.length;
let m = {};
for (let i = 0; i < B.length; i++) {
m[B[i]] = i;
}
for (let i in A) {
res[i] = m[A[i]];
}
return res;
};
let A = [ 12 , 28 46 32 50 ;
let B = [50, 12, 32, 46, 28];
let res = anagramMappings(A, B);
console.log(res);
From for notes (Wiz)
760. Find Anagram Mappings Lookup mappings