The provided solution is already quite good, clear, and efficient (linear time complexity with respect to the number of digits). The use of str.replace() is idiomatic Python for this kind of operation.
However, we can make it slightly more concise by consolidating the "no change" logic using conditional expressions.
Here's a revised version that slightly simplifies the flow by explicitly setting the character to replace and the replacement character, then performing the replacement in a single step for both max_str and min_str:
class Solution:
def maxDiff(self, num: int) -> int:
str_num = str(num)
# --- Calculate max_str ---
# Goal: Find the leftmost digit that is not '9', and replace all occurrences of it with '9'.
# If all digits are '9', no change is made.
char_to_replace_for_max = ''
for char_val in str_num:
if char_val != '9':
char_to_replace_for_max = char_val
break
# Perform replacement if a suitable character was found, otherwise keep original
max_val_str = str_num.replace(char_to_replace_for_max, '9') if char_to_replace_for_max else str_num
# --- Calculate min_str ---
# Goal:
# 1. If the first digit is not '1', replace all occurrences of it with '1'.
# 2. Else (if the first digit is '1'), find the leftmost digit (from the second position onwards)
# that is neither '0' nor '1', and replace all occurrences of it with '0'.
# If no suitable digit found based on these rules, no change is made.
char_to_replace_for_min = ''
replace_with_for_min = ''
if str_num[0] != '1':
char_to_replace_for_min = str_num[0]
replace_with_for_min = '1'
else: # The first digit is '1'
# Look for a digit from the second position onwards that is not '0' and not '1'
for i in range(1, len(str_num)):
char_val = str_num[i]
if char_val != '0' and char_val != '1':
char_to_replace_for_min = char_val
replace_with_for_min = '0'
break
# Perform replacement if a suitable character was found, otherwise keep original
min_val_str = str_num.replace(char_to_replace_for_min, replace_with_for_min) if char_to_replace_for_min else str_num
return int(max_val_str) - int(min_val_str)
Why this is slightly "simpler" (or at least cleaner):
- Unified Replacement Logic: Instead of having
str_num.replace(...) inside if/else blocks and then separate max_str = str_num lines for the "no change" cases, we first determine what should be replaced (and with what), and then perform the replace operation in a single line using a conditional expression (if ... else ...). This removes one level of indentation for the "no change" scenarios.
- Clarity on Variable Roles:
char_to_replace_for_max, char_to_replace_for_min, replace_with_for_min clearly indicate their purpose.
- Readability: Breaking down the logic for
max_str and min_str into distinct blocks with comments explaining the goals improves readability.
The core logic remains the same, as the problem inherently requires these specific conditional checks for building the largest and smallest numbers under the given rules. This refactor makes the implementation of that logic slightly more elegant.