Determine equality after limited swaps
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates string manipulation and combinatorial reasoning about character swaps and anagram formation, focusing on competencies such as frequency analysis and reasoning about minimal edit operations.
Constraints
- 1 <= len(s) = len(t) <= 200000
- s and t consist only of lowercase English letters ('a'-'z')
- 0 <= k <= 10^12
Hints
- If s and t do not have identical character frequencies, it is impossible.
- Match the k-th occurrence of each character in s to the k-th occurrence of the same character in t to form a permutation of target indices.
- The minimum number of adjacent swaps equals the inversion count of that permutation.
- Use a Binary Indexed Tree (Fenwick Tree) or a mergesort-based approach to count inversions in O(n log n).