-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2259-RemoveDigitFromNumberToMaximizeResult.go
More file actions
66 lines (57 loc) · 2.14 KB
/
2259-RemoveDigitFromNumberToMaximizeResult.go
File metadata and controls
66 lines (57 loc) · 2.14 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
// 2259. Remove Digit From Number to Maximize Result
// You are given a string number representing a positive integer and a character digit.
// Return the resulting string after removing exactly one occurrence of digit from number
// such that the value of the resulting string in decimal form is maximized.
// The test cases are generated such that digit occurs at least once in number.
// Example 1:
// Input: number = "123", digit = "3"
// Output: "12"
// Explanation: There is only one '3' in "123". After removing '3', the result is "12".
// Example 2:
// Input: number = "1231", digit = "1"
// Output: "231"
// Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".
// Since 231 > 123, we return "231".
// Example 3:
// Input: number = "551", digit = "5"
// Output: "51"
// Explanation: We can remove either the first or second '5' from "551".
// Both result in the string "51".
// Constraints:
// 2 <= number.length <= 100
// number consists of digits from '1' to '9'.
// digit is a digit from '1' to '9'.
// digit occurs at least once in number.
import "fmt"
func removeDigit(number string, digit byte) string {
res := ""
for i := 0; i < len(number); i++ {
if number[i] == digit {
dummy := number[:i] + number[i+1:]
if dummy > res {
res = dummy
}
}
}
return res
}
func main() {
// Example 1:
// Input: number = "123", digit = "3"
// Output: "12"
// Explanation: There is only one '3' in "123". After removing '3', the result is "12".
fmt.Println(removeDigit("123", '3')) // "12"
// Example 2:
// Input: number = "1231", digit = "1"
// Output: "231"
// Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".
// Since 231 > 123, we return "231".
fmt.Println(removeDigit("1231", '1')) // "231"
// Example 3:
// Input: number = "551", digit = "5"
// Output: "51"
// Explanation: We can remove either the first or second '5' from "551".
// Both result in the string "51".
fmt.Println(removeDigit("551", '5')) // "51"
}