Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Build confidence with an intuition-first walkthrough focused on greedy fundamentals.
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 <= 100number consists of digits from '1' to '9'.digit is a digit from '1' to '9'.digit occurs at least once in number.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"123" "3"
"1231" "1"
"551" "5"
remove-k-digits)remove-vowels-from-a-string)second-largest-digit-in-a-string)minimum-operations-to-make-a-special-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2259: Remove Digit From Number to Maximize Result
class Solution {
public String removeDigit(String number, char digit) {
String ans = "0";
for (int i = 0, n = number.length(); i < n; ++i) {
char d = number.charAt(i);
if (d == digit) {
String t = number.substring(0, i) + number.substring(i + 1);
if (ans.compareTo(t) < 0) {
ans = t;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2259: Remove Digit From Number to Maximize Result
func removeDigit(number string, digit byte) string {
ans := "0"
for i, d := range number {
if d == rune(digit) {
t := number[:i] + number[i+1:]
if strings.Compare(ans, t) < 0 {
ans = t
}
}
}
return ans
}
# Accepted solution for LeetCode #2259: Remove Digit From Number to Maximize Result
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
return max(
number[:i] + number[i + 1 :] for i, d in enumerate(number) if d == digit
)
// Accepted solution for LeetCode #2259: Remove Digit From Number to Maximize Result
/**
* [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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/
// discuss: https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn remove_digit(number: String, digit: char) -> String {
let mut result = String::new();
for (i, v) in number.chars().enumerate() {
if v == digit {
let mod_number = format!("{}{}", &number[..i], &number[i + 1..]);
if mod_number > result {
result = mod_number;
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2259_example_1() {
let number = "123".to_string();
let digit = '3';
let result = "12".to_string();
assert_eq!(Solution::remove_digit(number, digit), result);
}
#[test]
fn test_2259_example_2() {
let number = "1231".to_string();
let digit = '1';
let result = "231".to_string();
assert_eq!(Solution::remove_digit(number, digit), result);
}
#[test]
fn test_2259_example_3() {
let number = "551".to_string();
let digit = '5';
let result = "51".to_string();
assert_eq!(Solution::remove_digit(number, digit), result);
}
}
// Accepted solution for LeetCode #2259: Remove Digit From Number to Maximize Result
function removeDigit(number: string, digit: string): string {
const n = number.length;
let last = -1;
for (let i = 0; i < n; ++i) {
if (number[i] === digit) {
last = i;
if (i + 1 < n && number[i] < number[i + 1]) {
break;
}
}
}
return number.substring(0, last) + number.substring(last + 1);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.