2259. Remove Digit From Number to Maximize Result LeetCode Solution

In this guide, you will get 2259. Remove Digit From Number to Maximize Result LeetCode Solution with the best time and space complexity. The solution to Remove Digit From Number to Maximize Result problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove Digit From Number to Maximize Result solution in C++
  4. Remove Digit From Number to Maximize Result solution in Java
  5. Remove Digit From Number to Maximize Result solution in Python
  6. Additional Resources
2259. Remove Digit From Number to Maximize Result LeetCode Solution image

Problem Statement of 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”.

See also  781. Rabbits in Forest LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2259. Remove Digit From Number to Maximize Result LeetCode Solution in C++

class Solution {
 public:
  string removeDigit(string number, char digit) {
    for (int i = 0; i + 1 < number.length(); ++i)
      if (number[i] == digit && digit < number[i + 1])
        return number.erase(i, 1);
    return number.erase(number.rfind(digit), 1);
  }
};
/* code provided by PROGIEZ */

2259. Remove Digit From Number to Maximize Result LeetCode Solution in Java

class Solution {
  public String removeDigit(String number, char digit) {
    for (int i = 0; i + 1 < number.length(); ++i)
      if (number.charAt(i) == digit && digit < number.charAt(i + 1))
        return new StringBuilder(number).deleteCharAt(i).toString();
    return new StringBuilder(number).deleteCharAt(number.lastIndexOf(digit)).toString();
  }
}
// code provided by PROGIEZ

2259. Remove Digit From Number to Maximize Result LeetCode Solution in Python


# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.