You are given an integer num. You will apply the following steps exactly two times:

Pick a digit x (0 <= x <= 9).
Pick another digit y (0 <= y <= 9). The digit y can be equal to x.
Replace all the occurrences of x in the decimal representation of num by y.
The new integer cannot have any leading zeros, also the new integer cannot be 0.

Let a and b be the results of applying the operations to num the first and second times, respectively.
Return the max difference between a and b.
 
Example 1:
Input: num = 555
Output: 888
Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
The second time pick x = 5 and y = 1 and store the new integer in b.
We have now a = 999 and b = 111 and max difference = 888

Example 2:
Input: num = 9
Output: 8
Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
The second time pick x = 9 and y = 1 and store the new integer in b.
We have now a = 9 and b = 1 and max difference = 8

Example 3:
Input: num = 123456
Output: 820000

Example 4:
Input: num = 10000
Output: 80000

Example 5:
Input: num = 9288
Output: 8700

 
Constraints:

1 <= num <= 10^8

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Mathematics

Greedy algorithms

Starter code: class Solution:
    def maxDiff(self, num: int) -> int:
        

Solutions:
class Solution:

	def maxDiff(self, num: int) -> int:
		if num < 10:
			return 8
		a = b = str(num)
		i = 0
		while i < len(a):
			if a[i] == '9':
				i += 1
			else:
				a = a.replace(a[i], '9')
				break
		if b[0] != '1':
			b = b.replace(b[0], '1')
		else:
			i = 1
			while i < len(b):
				if b[i] == '1' or b[i] == '0':
					i += 1
				else:
					b = b.replace(b[i], '0')
					break
		return int(a) - int(b)


class Solution:

	def maxDiff(self, num: int) -> int:
		num = [int(c) for c in str(num)]
		(max_num, min_num) = (num[:], num[:])
		for v in num:
			if v < 9:
				max_num = [digit if digit != v else 9 for digit in num]
				break
		if num[0] != 1:
			min_num = [digit if digit != num[0] else 1 for digit in num]
		else:
			for v in num[1:]:
				if v > 1:
					min_num = [digit if digit != v else 0 for digit in num]
					break
		print((max_num, min_num))
		return int(''.join([str(digit) for digit in max_num])) - int(''.join([str(digit) for digit in min_num]))


class Solution:

	def maxDiff(self, num: int) -> int:
		replaces = []
		num = str(num)
		for i in range(0, 10):
			for j in range(0, 10):
				rep_num = num.replace(str(i), str(j))
				if rep_num[0] != '0':
					replaces.append(int(rep_num))
		return max(replaces) - min(replaces)


class Solution:

	def maxDiff(self, num: int) -> int:
		numStr1 = list(str(num))
		numStr2 = list(str(num))
		numStrLen = len(numStr1)
		firstChar = numStr1[0]
		for (i, char) in enumerate(numStr1):
			if char != firstChar and char > '0':
				for j in range(i, numStrLen):
					if numStr1[j] == char:
						numStr1[j] = '0'
				break
			if char == firstChar and char > '1':
				for j in range(i, numStrLen):
					if numStr1[j] == char:
						numStr1[j] = '1'
				break
		for (i, char) in enumerate(numStr2):
			if char < '9':
				for j in range(i, numStrLen):
					if numStr2[j] == char:
						numStr2[j] = '9'
				break
		return int(''.join(numStr2)) - int(''.join(numStr1))


class Solution:

	def maxDiff(self, num: int) -> int:
		return self.findMax(num) - self.findMin(num)

	def findMax(self, num):
		s = str(num)
		org = None
		new_s = ''
		for c in s:
			if c == '9':
				new_s += c
				continue
			if org is None:
				org = c
			if c == org:
				new_s += '9'
			else:
				new_s += c
		return int(new_s)

	def findMin(self, num):
		s = str(num)
		org = None
		rep = None
		new_s = ''
		for i in range(len(s)):
			if s[i] == '0':
				new_s += s[i]
				continue
			if i == 0:
				if s[0] == '1':
					new_s += s[0]
				else:
					org = s[0]
					rep = '1'
					new_s += rep
			elif org is None and s[i] != '1':
				org = s[i]
				rep = '0'
				new_s += rep
			elif s[i] == org:
				new_s += rep
			else:
				new_s += s[i]
		return int(new_s)


class Solution:

	def maxDiff(self, num: int) -> int:
		s = str(num)
		a = True
		i = 0
		while i < len(s):
			if a and int(s[i]) < 9:
				n = s[i]
				a = False
			if not a:
				if s[i] == n:
					s = s[:i] + '9' + s[i + 1:]
			i += 1
		n1 = int(s)
		s = str(num)
		a = True
		if int(s[0]) > 1:
			n = s[0]
			y = '1'
			s = y + s[1:]
			a = False
		i = 1
		while i < len(s):
			if a and int(s[i]) > 0:
				if s[i] != s[0]:
					n = s[i]
					y = '0'
					a = False
			if not a:
				if s[i] == n:
					s = s[:i] + y + s[i + 1:]
			i += 1
		n2 = int(s)
		return n1 - n2


class Solution:

	def maxDiff(self, num: int) -> int:
		if num < 10:
			return 8
		numStr = str(num)
		(a, b) = ([], [])
		for i in range(len(numStr)):
			a.append(numStr[i])
			b.append(numStr[i])
		for i in range(len(a)):
			temp = a[i]
			if not a[i] == '9':
				for j in range(len(a)):
					if a[j] == temp:
						a[j] = '9'
				break
		for i in range(len(b)):
			if i == 0:
				temp = b[0]
				if not b[0] == '1':
					for j in range(len(b)):
						if b[j] == temp:
							b[j] = '1'
					break
			else:
				temp = b[i]
				if not b[i] == '0' and (not b[i] == '1'):
					for j in range(len(b)):
						if b[j] == temp:
							b[j] = '0'
					break
		(c, d) = ('', '')
		for i in range(len(numStr)):
			(c, d) = (c + a[i], d + b[i])
		return int(c) - int(d)


class Solution:

	def maxDiff(self, num: int) -> int:
		maxi = '-inf'
		mini = 'inf'
		num = str(num)
		leading = num[0]
		for a in set(num):
			a = str(a)
			for b in range(10):
				if not b and a == leading:
					continue
				b = str(b)
				temp = num.replace(a, b)
				mini = min(mini, temp)
				maxi = max(maxi, temp)
		return int(maxi) - int(mini)


class Solution:

	def maxDiff(self, num: int) -> int:
		a = []
		s = str(num)
		for i in range(0, 10):
			for j in s:
				n = s.replace(j, str(i))
				m = int(n)
				if m != 0 and n[0] != '0':
					a.append(m)
		diff = max(a) - min(a)
		return diff