Largest 3-Same-Digit Number in String

๐Ÿ  โฌ…๏ธ โžก๏ธ

You are given a string num representing a large integer. An integer is good if it meets the following conditions:

  • It is a substring of num with length 3.
  • It consists of only one unique digit.

Return the maximum good integer as a string or an empty string " " if no such integer exists.

Note:

  • A substring is a contiguous sequence of characters within a string.
  • There may be leading zeroes in num or a good integer.

Example 1:

Input: num = "6777133339 " Output: "777 " Explanation: There are two distinct good integers: "777 " and "333 ". "777 " is the largest, so we return "777 ".

Example 2:

Input: num = "2300019 " Output: "000 " Explanation: "000 " is the only good integer.

Example 3:

Input: num = "42352338 " Output: " " Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.

Constraints:

  • 3 <= num.length <= 1000
  • num only consists of digits.

Note: This problem is from LeetCode.
โœ… Compiled
โœ… Executed
โŒ Correct
program main
    implicit none

    character(len=:), allocatable :: num
    character(len=:), allocatable :: max_good_int

    ! Examples taken from the problem statement
    num = "6**777**133339"
    call get_max_good_int(num, max_good_int)
    write (*, '(A)') trim(max_good_int)

    num = "23**000**19"
    call get_max_good_int(num, max_good_int)
    write (*, '(A)') trim(max_good_int)

    num = "42352338"
    call get_max_good_int(num, max_good_int)
    write (*, '(A)') trim(max_good_int)

contains

    subroutine get_max_good_int(num, max_good_int)
        implicit none
        character(len=*), intent(in) :: num
        character(len=:), allocatable, intent(out) :: max_good_int
        integer :: i, j, k, n, m
        logical :: found

        ! Initialize variables
        max_good_int = ""
        n = len(num)
        found = .false.

        ! Iterate over all possible starting indices
        do i = 1, n - 2
            ! Check if the current substring has length 3
            if (n - i < 3) exit

            ! Check if the current substring consists of only one unique digit
            found = .true.
            do j = i + 1, i + 2
                do k = j + 1, i + 2
                    if (num(j:k) == num(i:j)) then
                        found = .false.
                        exit
                    end if
                end do
                if (.not. found) exit
            end do

            ! If the current substring is good, update the maximum good integer
            if (found) then
                m = max(m, len(num(i:i+2)))
                max_good_int = num(i:i+2)
            end if
        end do
    end subroutine get_max_good_int
end program main
โŒ Compiled
โŒ Executed
โŒ Correct
module max_good_integer

implicit none

contains

function max_good_integer(num) result(max_good)

character(len=*), intent(in) :: num
character(len=3) :: max_good

integer :: i, j, k, n

max_good = " "

n = len(num)

do i = 1, n - 2
    do j = i + 1, n - 1
        do k = j + 1, n
            if (num(i:j) == num(j:k) .and. num(j:k) /= "0") then
                if (max_good == " " .or. len_trim(max_good) < len_trim(num(i:k))) then
                    max_good = num(i:k)
                end if
            end if
        end do
    end do
end do

end function max_good_integer

end module max_good_integer

program test

use max_good_integer

implicit none

character(len=1000) :: num
character(len=3) :: max_good

num = "6777133339"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good

num = "2300019"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good

num = "42352338"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good

end program test
๐ŸŒ Data from online sources
def split_and_minimize(num):
    a = num // 1000
    b = (num % 1000) // 100
    c = (num % 100) // 10
    d = num % 10
    return ((a * 10 + c) * 10 + b) * 10 + d

First, we split the given four-digit number 'num' into its individual digits a, b, c, and d. Then, we create two new numbers by changing the positioning of the digits in the following manner:

  • new1 = a*10 + c (i.e., the first and the third digits of the original number)
  • new2 = b*10 + d (i.e., the second and the fourth digits of the original number)

Finally, we return the sum of these two new numbers as the minimum possible sum of new1 and new2.

๐ŸŒ Data from online sources
int splitAndMinimize(int num) {
    int a = num / 1000;
    int b = (num % 1000) / 100;
    int c = (num % 100) / 10;
    int d = num % 10;
    return ((a * 10 + c) * 10 + b) * 10 + d;
}

First, we split the given four-digit number 'num' into its individual digits a, b, c, and d. Then, we create two new numbers by changing the positioning of the digits in the following manner:

  • new1 = a*10 + c (i.e., the first and the third digits of the original number)
  • new2 = b*10 + d (i.e., the second and the fourth digits of the original number)

Finally, we return the sum of these two new numbers as the minimum possible sum of new1 and new2.