NeetCode #11LC-760EasyArrays & HashingNC Algo100
← Back to All Problems#11 · #760 · Find Anagram Mappings(找出变位映射)
📌 Problem Statement & Constraints
Given two arrays
nums1 and nums2 that are anagrams of each other, return a mapping array mapping where mapping[i] == j means nums2[j] == nums1[i]. If there are multiple answers, return any of them. Constraints: 1 <= nums1.length == nums2.length <= 100, 0 <= nums1[i], nums2[i] <= 10^5.💡 Core Algorithmic Approaches
- For each value in
nums1, we need some index innums2holding that value. Since values may repeat, store a list of indices per value. - Build a map from value to the list of positions where it occurs in
nums2. - Walk
nums1in order and consume one position from the corresponding list each time. - A per-value cursor ensures each occurrence in
nums1maps to a distinct occurrence innums2, which is what the problem requires.
💻 Benchmark Python3 Implementation
class Solution:
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
from collections import defaultdict
pos = defaultdict(list)
for j, v in enumerate(nums2):
pos[v].append(j) # all positions of each value
used = defaultdict(int) # how many we have consumed so far
res = []
for v in nums1:
res.append(pos[v][used[v]]) # take the next unused position
used[v] += 1
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): one pass to index nums2, one pass over nums1.
💾 Space Complexity
O(n) for the position lists.
⚠️ Interview Pitfalls & Follow-ups
- Using a plain value-to-index dict: duplicates in
nums2would overwrite each other, and two equal values innums1would map to the same index, which the problem forbids. - Assuming the mapping is unique: the problem explicitly allows any valid answer, so do not try to compute a canonical one.
- Sorting both arrays and mapping by rank: it works, but O(n log n) and more code than the direct index lists.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
找出变位映射
给定两个整数数组 nums1 和 nums2,其中 nums2 是 nums1 的一个变位(即元素构成完全相同,仅顺序不同)。请找出一个下标映射数组 mapping,使得 nums1[i] == nums2[mapping[i]] 。如果存在多个答案,返回其中任意一个即可。约束:1 ≤ nums1.length == nums2.length ≤ 100;0 ≤ nums1[i], nums2[i] ≤ 10^5。
核心要点
- 哈希表记录位置(标准解) :先遍历 nums2,建立「值 → 该值在 nums2 中出现过的下标列表」。
- 为什么需要「下标列表」而不是单个下标 :数组可能含重复元素(如 nums2 = [1, 1]),一个值对应多个合法位置,因此需要存列表。
- 贪心配对 :遍历 nums1,对每个值从它的下标列表中取出一个(弹出即可),放入答案。因为 nums2 是 nums1 的变位,列表长度必然够用。
- 正确性论证 :每个值 v 在 nums1 与 nums2 中出现次数相同,因此逐个消耗列表不会出现「取空」的情况。
常见陷阱
- 用「值 → 单个下标」的字典 :nums2 含重复元素时,后出现的下标会覆盖先出现的,导致重复元素全部映射到同一下标,答案错误。
- 误以为映射唯一 :题目明确允许多解,不需要找「最小」或「字典序最小」的映射,任意合法映射均可。
- 用 index() 每次查找 : nums2.index(v) 是 O(n) 查找,整体退化为 O(n^2)。建哈希表才是 O(n)。
参考实现
from collections import defaultdict
class Solution:
def anagramMappings(self, nums1: list[int], nums2: list[int]) -> list[int]:
pos = defaultdict(list)
for i, v in enumerate(nums2):
pos[v].append(i) # 值 -> 所有出现的下标
return [pos[v].pop() for v in nums1] # 逐个消耗,保证重复元素也能正确配对