LeetCode 2259: Remove Digit From Number to Maximize Result

Problem Description

Explanation:

To maximize the resulting number, we can remove the first occurrence of the given digit, as removing any other occurrence would result in a smaller number. We can find the index of the first occurrence of the digit in the number and then remove it to obtain the maximum possible number.

  • Find the index of the first occurrence of the digit in the number.
  • Remove the digit at that index to obtain the maximum number.

Time Complexity: O(n) where n is the length of the input number.

Space Complexity: O(1)

Solutions

class Solution {
    public String removeDigit(String number, char digit) {
        int index = number.indexOf(digit);
        return number.substring(0, index) + number.substring(index + 1);
    }
}

Loading editor...