Skip to content

2259. Remove Digit From Number to Maximize Result

Description

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.

Solutions

Solution 1: Brute Force Enumeration

Thinking

We must delete exactly one occurrence of a given digit to maximize the remaining decimal string. The length is at most \(100\), so building the string for every deletion is enough.

Enumerate indices \(i\) equal to \(\textit{digit}\) and take the maximum of \(number[:i]+number[i+1:]\). Lexicographic order matches numeric order.

We can enumerate all positions \(\textit{i}\) in the string \(\textit{number}\). If \(\textit{number}[i] = \textit{digit}\), we take the prefix \(\textit{number}[0:i]\) and the suffix \(\textit{number}[i+1:]\) of \(\textit{number}\) and concatenate them. This gives the result after removing \(\textit{number}[i]\). We then take the maximum of all possible results.

The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the string \(\textit{number}\).

1
2
3
4
5
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
        )
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
public:
    string removeDigit(string number, char digit) {
        string ans = "0";
        for (int i = 0, n = number.size(); i < n; ++i) {
            char d = number[i];
            if (d == digit) {
                string t = number.substr(0, i) + number.substr(i + 1, n - i);
                if (ans < t) {
                    ans = t;
                }
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    /**
     * @param String $number
     * @param String $digit
     * @return String
     */
    function removeDigit($number, $digit) {
        $max = 0;
        for ($i = 0; $i < strlen($number); $i++) {
            if ($number[$i] == $digit) {
                $tmp = substr($number, 0, $i) . substr($number, $i + 1);
                if ($tmp > $max) {
                    $max = $tmp;
                }
            }
        }
        return $max;
    }
}

Solution 2: Greedy

Thinking

Solution 1 builds many strings. Walking left to right, if an occurrence of \(\textit{digit}\) is smaller than the next character, deleting it raises a more significant digit and is optimal. Otherwise delete the last occurrence so a high digit is not replaced by a smaller one.

Track the last index and stop early on a strict ascent. The scan is linear.

We can enumerate all positions \(\textit{i}\) in the string \(\textit{number}\). If \(\textit{number}[i] = \textit{digit}\), we record the last occurrence position of \(\textit{digit}\) as \(\textit{last}\). If \(\textit{i} + 1 < \textit{n}\) and \(\textit{number}[i] < \textit{number}[i + 1]\), then we can directly return \(\textit{number}[0:i] + \textit{number}[i+1:]\) as the result after removing \(\textit{number}[i]\). This is because if \(\textit{number}[i] < \textit{number}[i + 1]\), removing \(\textit{number}[i]\) will result in a larger number.

After the traversal, we return \(\textit{number}[0:\textit{last}] + \textit{number}[\textit{last}+1:]\).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def removeDigit(self, number: str, digit: str) -> str:
        last = -1
        n = len(number)
        for i, d in enumerate(number):
            if d == digit:
                last = i
                if i + 1 < n and d < number[i + 1]:
                    break
        return number[:last] + number[last + 1 :]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public String removeDigit(String number, char digit) {
        int last = -1;
        int n = number.length();
        for (int i = 0; i < n; ++i) {
            char d = number.charAt(i);
            if (d == digit) {
                last = i;
                if (i + 1 < n && d < number.charAt(i + 1)) {
                    break;
                }
            }
        }
        return number.substring(0, last) + number.substring(last + 1);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    string removeDigit(string number, char digit) {
        int n = number.size();
        int last = -1;
        for (int i = 0; i < n; ++i) {
            char d = number[i];
            if (d == digit) {
                last = i;
                if (i + 1 < n && number[i] < number[i + 1]) {
                    break;
                }
            }
        }
        return number.substr(0, last) + number.substr(last + 1);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func removeDigit(number string, digit byte) string {
    last := -1
    n := len(number)
    for i := range number {
        if number[i] == digit {
            last = i
            if i+1 < n && number[i] < number[i+1] {
                break
            }
        }
    }
    return number[:last] + number[last+1:]
}

Comments