Skip to content

Commit 49b9939

Browse files
committed
2259. Remove Digit From Number to Maximize Result: AC
1 parent be76538 commit 49b9939

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,3 +1709,4 @@ mod s2255_count_prefixes_of_a_given_string;
17091709
mod s2256_minimum_average_difference;
17101710
mod s2257_count_unguarded_cells_in_the_grid;
17111711
mod s2258_escape_the_spreading_fire;
1712+
mod s2259_remove_digit_from_number_to_maximize_result;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
*/
36+
pub 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+
43+
impl Solution {
44+
pub fn remove_digit(number: String, digit: char) -> String {
45+
let mut result = String::new();
46+
47+
for (i, v) in number.chars().enumerate() {
48+
if v == digit {
49+
let mod_number = format!("{}{}", &number[..i], &number[i + 1..]);
50+
if mod_number > result {
51+
result = mod_number;
52+
}
53+
}
54+
}
55+
56+
result
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_2259_example_1() {
68+
let number = "123".to_string();
69+
let digit = '3';
70+
71+
let result = "12".to_string();
72+
73+
assert_eq!(Solution::remove_digit(number, digit), result);
74+
}
75+
76+
#[test]
77+
fn test_2259_example_2() {
78+
let number = "1231".to_string();
79+
let digit = '1';
80+
81+
let result = "231".to_string();
82+
83+
assert_eq!(Solution::remove_digit(number, digit), result);
84+
}
85+
86+
#[test]
87+
fn test_2259_example_3() {
88+
let number = "551".to_string();
89+
let digit = '5';
90+
91+
let result = "51".to_string();
92+
93+
assert_eq!(Solution::remove_digit(number, digit), result);
94+
}
95+
}

0 commit comments

Comments
 (0)