Ctrl K

Spread After Swapping One Digit

Easy
You are given an integer number. You may pick one digit a and one digit b and replace **every** occurrence of a in the number with b. Picking b = a is allowed and leaves the number unchanged. Leading zeros after the replacement are fine: 0020 simply means 20. First use such a replacement to get the largest possible number, then, separately and possibly with a different replacement, the smallest one. Return the difference between them.
Example 1
number=4024 → 9009

Maximum: turn every 4 into 9 to get 9029. Minimum: turn every 4 into 0 to get 0020, which is 20. The spread is 9029 - 20 = 9009.

Example 2
number=909 → 999

Maximum: turn 0 into 9 to get 999. Minimum: turn 9 into 0 to get 000, which is 0. The answer is 999.

Example 3
number=7 → 9
Constraints
  • 1 ≤ number ≤ 10¹⁰
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriangle From Sticks EasyHopping Tokens Together EasyThe Biggest Odd Chunk EasyTwo Numbers From Four Digits EasySmallest Sum From Splitting the Digits Easy
loading Python…
Input
number=4024
Expected
9009

Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.