Find Anagram Mappings — LeetCode 760 Python Solution
EasyLeetCode PremiumArrayHash Table
- Problem
- #760
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. Both arrays may contain duplicates.
Example
- 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.
Python solution
Python
class Solution:
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
d = {x: i for i, x in enumerate(nums2)}
return [d[x] for x in nums1]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n), where n is the length of the array auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 760. Find Anagram Mappings is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 760. Find Anagram Mappings?
- LeetCode 760. Find Anagram Mappings is rated Easy on LeetCode.
- What is the time complexity of LeetCode 760. Find Anagram Mappings?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 760. Find Anagram Mappings?
- The Python solution on this page uses O(n), where n is the length of the array auxiliary space.
- What topics does LeetCode 760. Find Anagram Mappings cover?
- LeetCode 760. Find Anagram Mappings is tagged Array and Hash Table on LeetCode.
- Is LeetCode 760. Find Anagram Mappings a premium problem?
- Yes. LeetCode 760. Find Anagram Mappings is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.