760. Find Anagram Mappings Lookup mappings

Source: Internet
Author: User

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] = 1Because the 0th element of AAppears at B[1], and P[1] = 4Because the 1St element of AAppears at B[4], and so on.

Note:

    1. A, Bhas equal lengths in range [1, 100] .
    2. 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.

  
 
  1. /**
  2. * @param {number[]} A
  3. * @param {number[]} B
  4. * @return {number[]}
  5. */
  6. var anagramMappings = function (A, B) {
  7. let res = [];
  8. res.length = A.length;
  9. let m = {};
  10. for (let i = 0; i < B.length; i++) {
  11. m[B[i]] = i;
  12. }
  13. for (let i in A) {
  14. res[i] = m[A[i]];
  15. }
  16. return res;
  17. };
  18. let A = [ 12 , 28 46 32 50 ;
  19. let B = [50, 12, 32, 46, 28];
  20. let res = anagramMappings(A, B);
  21. console.log(res);




From for notes (Wiz)

760. Find Anagram Mappings Lookup mappings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.