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 ith 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.

Example 1:

Input: nums1 = [12,28,46,32,50], nums2 = [50,12,32,46,28] Output: [1,4,3,2,0] Explanation: As mapping[0] = 1 because the 0th element of nums1 appears at nums2[1], and mapping[1] = 4 because the 1st element of nums1 appears at nums2[4], and so on.

Example 2:

Input: nums1 = [84,46], nums2 = [84,46] Output: [0,1]

Constraints:

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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 5
    integer, dimension(n) :: nums1 = [12, 28, 46, 32, 50]
    integer, dimension(n) :: nums2 = [50, 12, 32, 46, 28]
    integer, dimension(n) :: mapping

    call solve(nums1, nums2, mapping)

    write (*, '(A)') 'Mapping:'
    write (*, '(*(I0, 1X))') mapping

contains

    subroutine solve(nums1, nums2, mapping)
        implicit none
        integer, intent(in) :: nums1(:), nums2(:)
        integer, intent(out) :: mapping(:)
        logical, allocatable :: seen(:)
        integer :: i, j, k

        allocate (seen(size(nums1)))

        do i = 1, size(nums1)
            seen(i) = .false.
        end do

        do i = 1, size(nums1)
            if (.not. seen(i)) then
                k = 1
                do while (k <= size(nums1))
                    if (nums1(i) == nums2(k)) then
                        seen(i) = .true.
                        exit
                    end if
                    k = k + 1
                end do
            end if
        end do

        do i = 1, size(nums1)
            j = 1
            do while (j <= size(nums1))
                if (nums1(i) == nums2(j)) then
                    mapping(i) = j
                    exit
                end if
                j = j + 1
            end do
        end do

    end subroutine solve

end program main
Compiled
Executed
Correct
module anagram_index_mapping

implicit none

contains

function anagram_index_mapping(nums1, nums2) result(mapping)

! This function returns an index mapping array mapping from nums1 to nums2
! where mapping[i] = j means the ith element in nums1 appears in nums2 at index j.
! If there are multiple answers, return any of them.

integer, intent(in) :: nums1(:), nums2(:)
integer :: mapping(size(nums1))

! Local variables
integer :: i, j, k, l, m
integer :: count(size(nums1))

! Initialize count array
count = 0

! Loop through nums2 and count the number of times each element appears
do i = 1, size(nums2)
    count(nums2(i)) = count(nums2(i)) + 1
end do

! Loop through nums1 and find the index of each element in nums2
do i = 1, size(nums1)
    ! Loop through nums2 and find the index of the current element in nums1
    do j = 1, size(nums2)
        if (nums1(i) == nums2(j)) then
            ! If the current element is found, decrement the count of that element
            count(nums2(j)) = count(nums2(j)) - 1
            ! And set the mapping for the current element
            mapping(i) = j
            exit
        end if
    end do
end do

end function anagram_index_mapping

end module anagram_index_mapping

program test_anagram_index_mapping

use anagram_index_mapping
implicit none

! Test case 1
integer, parameter :: nums1(5) = [12, 28, 46, 32, 50]
integer, parameter :: nums2(5) = [50, 12, 32, 46, 28]
integer :: mapping(size(nums1))

mapping = anagram_index_mapping(nums1, nums2)

! Print the mapping
write (*,*) "Test case 1:"
write (*,*) "nums1 = ", nums1
write (*,*) "nums2 = ", nums2
write (*,*) "mapping = ", mapping

! Test case 2
integer, parameter :: nums1(2) = [84, 46]
integer, parameter :: nums2(2) = [84, 46]

mapping = anagram_index_mapping(nums1, nums2)

! Print the mapping
write (*,*) "Test case 2:"
write (*,*) "nums1 = ", nums1
write (*,*) "nums2 = ", nums2
write (*,*) "mapping = ", mapping

end program test_anagram_index_mapping
🌐 Data from online sources
def anagramMappings(nums1, nums2):
    num_map = {num: i for i, num in enumerate(nums2)}
    mapping = [num_map[num] for num in nums1]
    return mapping
The algorithm involves creating a mapping between each value in `nums2` and its index, using a hash map. Then, for each element in `nums1`, get its corresponding index in `nums2` and append that index value into the resulting mapping. This approach has a linear computation complexity (O(n)), as iterating over both `nums1` and `nums2` only occurs once.
🌐 Data from online sources
#include <vector>
#include <unordered_map>
using namespace std;

vector<int> anagramMappings(vector<int>& nums1, vector<int>& nums2) {
    unordered_map<int, int> num_map;
    for (int i = 0; i < nums2.size(); i++) {
        num_map[nums2[i]] = i;
    }
    vector<int> mapping(nums1.size());
    for (int i = 0; i < nums1.size(); i++) {
        mapping[i] = num_map[nums1[i]];
    }
    return mapping;
}
The algorithm involves creating a mapping between each value in `nums2` and its index, using a hash map. Then, for each element in `nums1`, get its corresponding index in `nums2` and append that index value into the resulting mapping. This approach has a linear computation complexity (O(n)), as iterating over both `nums1` and `nums2` only occurs once.