2259. Remove Digit From Number to Maximize Result Easy

@problem@discussion
#String#Greedy#Enumeration



1/**
2 * [2259] Remove Digit From Number to Maximize Result
3 *
4 * You are given a string number representing a positive integer and a character digit.
5 * 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.
6 *  
7 * Example 1:
8 *
9 * Input: number = "123", digit = "3"
10 * Output: "12"
11 * Explanation: There is only one '3' in "123". After removing '3', the result is "12".
12 *
13 * Example 2:
14 *
15 * Input: number = "1231", digit = "1"
16 * Output: "231"
17 * Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".
18 * Since 231 > 123, we return "231".
19 *
20 * Example 3:
21 *
22 * Input: number = "551", digit = "5"
23 * Output: "51"
24 * Explanation: We can remove either the first or second '5' from "551".
25 * Both result in the string "51".
26 *
27 *  
28 * Constraints:
29 *
30 * 2 <= number.length <= 100
31 * number consists of digits from '1' to '9'.
32 * digit is a digit from '1' to '9'.
33 * digit occurs at least once in number.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/
39// discuss: https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn remove_digit(number: String, digit: char) -> String {
45        let chs: Vec<char> = number.chars().collect();
46        let mut last = number.len();
47
48        for i in 0..chs.len() - 1 {
49            if digit == chs[i] {
50                if (chs[i] as usize) < (chs[i + 1] as usize) {
51                    return Self::remove(number, i);
52                } else {
53                    last = i;
54                }
55            }
56        }
57
58        if chs[chs.len() - 1] == digit {
59            return Self::remove(number, chs.len() - 1);
60        } else {
61            return Self::remove(number, last);
62        }
63    }
64
65    fn remove(number: String, index: usize) -> String {
66        if index == 0 {
67            return number[1..].to_owned();
68        } else if index == number.len() {
69            return number[..number.len() - 1].to_owned();
70        } else {
71            return [&number[..index], &number[index + 1..]].join("").to_owned();
72        }
73    }
74}
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_2259_1() {
84        assert_eq!(
85            Solution::remove_digit(String::from("551"), '5'),
86            String::from("51")
87        );
88        assert_eq!(
89            Solution::remove_digit(String::from("1231"), '1'),
90            String::from("231")
91        );
92        assert_eq!(
93            Solution::remove_digit(String::from("4234234"), '4'),
94            String::from("423423")
95        );
96        assert_eq!(
97            Solution::remove_digit(String::from("423423"), '4'),
98            String::from("42323")
99        );
100    }
101}
102


Back
© 2025 bowen.ge All Rights Reserved.