Level Code Swap Count
Context
In a puzzle game, a player has a current level code and a required level code. One move may exchange the symbols at any two positions in the current code. Determine the fewest moves needed to reach the required code, or report that it cannot be reached.
Problem
Write a function that compares the current level code with the required level code. A move swaps the symbols at any two positions, with no restriction on their distance. Return the minimum number of moves needed to transform the current code into the required code. If transformation is impossible, return -1. The codes may be empty, and both inputs always have the same length. The result must depend only on the number of positions whose symbols differ, not on a particular sequence of swaps.
Examples
current_code = "10101"required_code = "10101"0current_code = "1100"required_code = "0011"2current_code = "000"required_code = "001"-1Constraints
0 <= len(current_code) <= 100000current_code: alphabet binarylen(current_code) <= len(required_code) <= len(current_code)required_code: alphabet binarycurrent_code and required_code contain binary characters only.- Types:
current_codeis str,required_codeis str; result is int
Function signature
def minimum_code_swaps(current_code: str, required_code: str) -> int
current_codestr- The player's current binary level code.
required_codestr- The binary level code needed to unlock the next level.
- returns int
- The minimum number of arbitrary swaps, or -1 when no sequence of swaps can produce the required code.
Adapted from MBPP problem task_595 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Swaps may use any two positions, including positions far apart.
- The infeasible result is -1.