Find Anagram Mappings
Problem
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
.
Constraints
1 <= nums1.length <= 100
nums2.length == nums1.length
0 <= nums1[i], nums2[i] <= 105
nums2
is an anagram ofnums1
.
Solution
The problem Find Anagram Mappings
can be solved by creating a queue of indexes for each elements.
Implementation
static const int fast_io = []()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution
{
public:
vector<int> anagramMappings(vector<int> &nums1, vector<int> &nums2)
{
unordered_map<int, queue<int>> hashmap;
for (int i = 0; i < nums2.size(); i++)
hashmap[nums2[i]].push(i);
vector<int> ret;
for (int &num : nums1)
{
ret.push_back(hashmap[num].front());
hashmap[num].pop();
}
return ret;
}
};