LeetCode solutions

2704. Maximum Difference by Remapping a Digit

My accepted Python solution to LeetCode problem 2704, Maximum Difference by Remapping a Digit, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.4MB, accepted 2026-01-01.

python
class Solution:
    def minMaxDifference(self, num: int) -> int:
        s = str(num)
        
        # For maximum: replace first non-9 digit with 9
        max_val = num
        for c in s:
            if c != '9':
                max_val = int(s.replace(c, '9'))
                break
        
        # For minimum: replace first digit with 0
        min_val = int(s.replace(s[0], '0'))
        
        return max_val - min_val

Source