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
.program main
implicit none
character(len=100) :: number
character(len=100) :: digit
character(len=100) :: result
! Examples
number = "123"
digit = "3"
call solve(number, digit, result)
write (*,*) trim(result)
number = "1231"
digit = "1"
call solve(number, digit, result)
write (*,*) trim(result)
number = "551"
digit = "5"
call solve(number, digit, result)
write (*,*) trim(result)
contains
subroutine solve(number, digit, result)
implicit none
character(len=*), intent(in) :: number
character(len=*), intent(in) :: digit
character(len=*), intent(out) :: result
integer :: i
integer :: j
integer :: n
integer :: d
n = len_trim(number)
d = len_trim(digit)
do i = 1, n - d + 1
if (number(i:i+d-1) == digit) then
result = number(:i-1) // number(i+d:)
exit
end if
end do
end subroutine solve
end program main
12 231 51
module MaximumValueAfterRemovingDigit
implicit none
private
public :: maximumValueAfterRemovingDigit
contains
function maximumValueAfterRemovingDigit(number, digit) result(result)
implicit none
character(len=*), intent(in) :: number
character(len=*), intent(in) :: digit
character(len=len(number)) :: result
integer :: i, j, k, n, d
logical :: found
! Initialize result to empty string
result = ""
! Find the first occurrence of digit in number
found = .false.
do i = 1, len(number)
if (number(i:i) == digit) then
found = .true.
exit
end if
end do
! If digit is not found in number, return empty string
if (.not. found) then
return
end if
! Copy the part of number before the first occurrence of digit
! to the result string
do i = 1, len(number)
if (i < i - 1) then
result(i:i) = number(i:i)
end if
end do
! Remove the first occurrence of digit from the result string
do i = 1, len(result)
if (result(i:i) == digit) then
result(i:i) = ""
exit
end if
end do
! Find the maximum value of the resulting string in decimal form
n = len(result)
d = 0
do i = n, 1, -1
d = d * 10 + ichar(result(i:i)) - ichar("0")
end do
! Return the maximum value
result = d
end function maximumValueAfterRemovingDigit
end module MaximumValueAfterRemovingDigit
program test_maximumValueAfterRemovingDigit
use MaximumValueAfterRemovingDigit
implicit none
integer :: i
do i = 1, size(test_cases)
write (*,*) "Test case #", i
write (*,*) "Input: ", test_cases(i)%number, ", ", test_cases(i)%digit
write (*,*) "Output: ", maximumValueAfterRemovingDigit(test_cases(i)%number, test_cases(i)%digit)
write (*,*)
end do
contains
type TestCase
character(len=100) :: number
character(len=100) :: digit
end type TestCase
type(TestCase), parameter :: test_cases(*) = &
(/TestCase("123 ", "3 "), &
TestCase("1231 ", "1 "), &
TestCase("551 ", "5 ")/)
end program test_maximumValueAfterRemovingDigit
temp.f95:4:44: 4 | public :: maximumValueAfterRemovingDigit | 1 Error: PUBLIC attribute applied to MODULE maximumvalueafterremovingdigit at (1) temp.f95:6:43: 6 | function maximumValueAfterRemovingDigit(number, digit) result(result) | 1 Error: MODULE attribute of ‘maximumvalueafterremovingdigit’ conflicts with PROCEDURE attribute at (1) temp.f95:7:21: 7 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:8:46: 8 | character(len=*), intent(in) :: number | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:45: 9 | character(len=*), intent(in) :: digit | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:26: 10 | character(len=len(number)) :: result | 1 Error: Symbol ‘number’ is used before it is typed at (1) temp.f95:11:32: 11 | integer :: i, j, k, n, d | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:24: 12 | logical :: found | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:19: 15 | result = "" | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:23: 18 | found = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:29: 19 | do i = 1, len(number) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:20:25: 20 | if (number(i:i) == digit) then | 1 Error: Syntax error in argument list at (1) temp.f95:21:30: 21 | found = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:20: 22 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:23:15: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:11: 24 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:29: 27 | if (.not. found) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:28:18: 28 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:29:11: 29 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:29: 33 | do i = 1, len(number) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:34:31: 34 | if (i < i - 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:36:15: 36 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:11: 37 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:40:29: 40 | do i = 1, len(result) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:41:25: 41 | if (result(i:i) == digit) then | 1 Error: Syntax error in argument list at (1) temp.f95:43:20: 43 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:44:15: 44 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:11: 45 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:48:23: 48 | n = len(result) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:49:13: 49 | d = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:50:23: 50 | do i = n, 1, -1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:51:40: 51 | d = d * 10 + ichar(result(i:i)) - ichar("0") | 1 Error: Syntax error in argument list at (1) temp.f95:52:11: 52 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:55:18: 55 | result = d | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:56:7: 56 | end function maximumValueAfterRemovingDigit | 1 Error: Expecting END MODULE statement at (1) temp.f95:60:9: 60 | use MaximumValueAfterRemovingDigit | 1 Fatal Error: Cannot open module file ‘maximumvalueafterremovingdigit.mod’ for reading at (1): No such file or directory compilation terminated.
def max_number_after_remove_digit(number: str, digit: str) -> str:
index = number.find(digit)
return number[:index] + number[index + 1:]
The algorithm for all the implementations is as follows:
digit
in the number
string.number
string (this is done implicitly for Java, Python, and JavaScript when manipulating strings), removing the digit at the found index.In all language implementations, we first use string methods like find
, indexOf
, or search
to find the index of the first occurrence of the digit in the number string. After that, we create a new string by removing the digit at that index using different string methods like erase
(in C++), substring
(in Java), string slicing (in Python), or slice
method (in JavaScript).
The time complexity for this algorithm is O(n) where n is the length of the number string. This is because finding the index of the digit and removing the digit from the string takes linear time.
std::string maxNumberAfterRemoveDigit(const std::string& number, const char digit) {
size_t index = number.find(digit);
std::string result = number;
result.erase(index, 1);
return result;
}
The algorithm for all the implementations is as follows:
digit
in the number
string.number
string (this is done implicitly for Java, Python, and JavaScript when manipulating strings), removing the digit at the found index.In all language implementations, we first use string methods like find
, indexOf
, or search
to find the index of the first occurrence of the digit in the number string. After that, we create a new string by removing the digit at that index using different string methods like erase
(in C++), substring
(in Java), string slicing (in Python), or slice
method (in JavaScript).
The time complexity for this algorithm is O(n) where n is the length of the number string. This is because finding the index of the digit and removing the digit from the string takes linear time.