-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2566_maximum_difference_by_remapping_a_digit.go
More file actions
46 lines (36 loc) · 1.11 KB
/
s2566_maximum_difference_by_remapping_a_digit.go
File metadata and controls
46 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
https://leetcode.com/problems/maximum-difference-by-remapping-a-digit/
You are given an integer num. You know that Bob will sneakily remap one of the
10 possible digits (0 to 9) to another digit.
Return the difference between the maximum and minimum values Bob can make by
remapping exactly one digit in num.
Notes:
When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences
of d1 in num with d2.
Bob can remap a digit to itself, in which case num does not change.
Bob can remap different digits for obtaining minimum and maximum values
respectively.
The resulting number after remapping can contain leading zeroes.
*/
package solutions
import (
"math"
"strconv"
"strings"
)
func minMaxDifference(num int) int {
s := strconv.Itoa(num)
maxVal := 0
minVal := math.MaxInt
for i := 0; i < len(s); i++ {
newS := strings.ReplaceAll(s[:], string(s[i]), "9")
n, _ := strconv.Atoi(newS)
maxVal = max(maxVal, n)
}
for i := 0; i < len(s); i++ {
newS := strings.ReplaceAll(s[:], string(s[i]), "0")
n, _ := strconv.Atoi(newS)
minVal = min(minVal, n)
}
return maxVal - minVal
}