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 <= 100nums2.length == nums1.length0 <= nums1[i], nums2[i] <= 105nums2 is an anagram of nums1.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
Mapping: 2 5 4 3 1
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
temp.f95:7:30:
7 | function anagram_index_mapping(nums1, nums2) result(mapping)
| 1
Error: MODULE attribute of ‘anagram_index_mapping’ conflicts with PROCEDURE attribute at (1)
temp.f95:13:41:
13 | integer, intent(in) :: nums1(:), nums2(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:14:24:
14 | integer :: mapping(size(nums1))
| 1
Error: Symbol ‘nums1’ is used before it is typed at (1)
temp.f95:17:24:
17 | integer :: i, j, k, l, m
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:18:22:
18 | integer :: count(size(nums1))
| 1
Error: Symbol ‘nums1’ is used before it is typed at (1)
temp.f95:21:9:
21 | count = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:24:21:
24 | do i = 1, size(nums2)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:25:41:
25 | count(nums2(i)) = count(nums2(i)) + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:26:3:
26 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:29:21:
29 | do i = 1, size(nums1)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:31:25:
31 | do j = 1, size(nums2)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:32:38:
32 | if (nums1(i) == nums2(j)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:34:49:
34 | count(nums2(j)) = count(nums2(j)) - 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:36:26:
36 | mapping(i) = j
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:37:16:
37 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:38:11:
38 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:39:7:
39 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:40:3:
40 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:42:3:
42 | end function anagram_index_mapping
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:48:5:
48 | use anagram_index_mapping
| 1
Fatal Error: Cannot open module file ‘anagram_index_mapping.mod’ for reading at (1): No such file or directory
compilation terminated.
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.
#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.