Check if Number Has Equal Digit Count and Digit Value

🏠 ⬅️ ➑️

You are given a 0-indexed string num of length n consisting of digits.

Return true if for every index i in the range 0 <= i < n, the digit i occurs num[i] times in num, otherwise return false.

Example 1:

Input: num = "1210 " Output: true Explanation: num[0] = '1'. The digit 0 occurs once in num. num[1] = '2'. The digit 1 occurs twice in num. num[2] = '1'. The digit 2 occurs once in num. num[3] = '0'. The digit 3 occurs zero times in num. The condition holds true for every index in "1210 ", so return true.

Example 2:

Input: num = "030 " Output: false Explanation: num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num. num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num. num[2] = '0'. The digit 2 occurs zero times in num. The indices 0 and 1 both violate the condition, so return false.

Constraints:

  • n == num.length
  • 1 <= n <= 10
  • num consists of digits.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=10) :: num
    integer :: n
    logical :: result

    ! Example 1
    num = "1210"
    n = len(trim(num))
    result = checkOccurrences(num, n)
    if (result) then
        print *, "Example 1: True"
    else
        print *, "Example 1: False"
    end if

    ! Example 2
    num = "030"
    n = len(trim(num))
    result = checkOccurrences(num, n)
    if (result) then
        print *, "Example 2: True"
    else
        print *, "Example 2: False"
    end if

contains

    function checkOccurrences(num, n) result(res)
        implicit none
        character(len=*), intent(in) :: num
        integer, intent(in) :: n
        logical :: res
        integer :: i, count

        do i = 0, n - 1
            count = 0
            do j = 1, n
                if (num(j:j) == char(48 + i)) then
                    count = count + 1
                end if
            end do
            if (count /= num(i+1:i+1)) then
                res = .false.
                return
            end if
        end do
        res = .true.
    end function checkOccurrences

end program main
❌ Compiled
❌ Executed
❌ Correct
module e99_every_digit_occurs_n_times

implicit none

interface

    function is_every_digit_occurs_n_times(num) result(res)
        implicit none
        character(len=*), intent(in) :: num
        logical :: res
    end function is_every_digit_occurs_n_times

end interface

contains

    function is_every_digit_occurs_n_times(num) result(res)
        implicit none
        character(len=*), intent(in) :: num
        integer :: i, j, n, digit
        logical :: res

        n = len(num)
        res = .true.

        do i = 0, n - 1
            digit = ichar(num(i + 1:i + 1)) - ichar('0')
            do j = 0, n - 1
                if (j /= i .and. num(j + 1:j + 1) == num(i + 1:i + 1)) then
                    res = .false.
                    exit
                end if
            end do
            if (.not. res) exit
        end do

    end function is_every_digit_occurs_n_times

end module e99_every_digit_occurs_n_times

program test_e99_every_digit_occurs_n_times
    use e99_every_digit_occurs_n_times
    implicit none
    logical :: res

    res = is_every_digit_occurs_n_times("1210")
    if (res) then
        write (*,*) "Passed!"
    else
        write (*,*) "Failed!"
    end if

    res = is_every_digit_occurs_n_times("030")
    if (res) then
        write (*,*) "Passed!"
    else
        write (*,*) "Failed!"
    end if

    res = is_every_digit_occurs_n_times("123456789")
    if (res) then
        write (*,*) "Passed!"
    else
        write (*,*) "Failed!"
    end if

    res = is_every_digit_occurs_n_times("0123456789")
    if (res) then
        write (*,*) "Passed!"
    else
        write (*,*) "Failed!"
    end if

end program test_e99_every_digit_occurs_n_times
🌐 Data from online sources
def rearrange_array(nums):
    nums.sort()
    for i in range(1, len(nums) - 1, 2):
        nums[i], nums[i + 1] = nums[i + 1], nums[i]
    return nums

The algorithm starts by sorting the given integer array nums in ascending order. Then, iterates through the sorted array using a for-loop, swapping the numbers at the even index with the number immediately after it. The next step in the loop is done by incrementing the iterator by 2 (i.e., i += 2). The algorithm continues until the entire array is rearranged. Finally, the rearranged array is returned.

🌐 Data from online sources
#include <vector>
#include <algorithm>
using namespace std;

vector<int> rearrangeArray(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    for (int i = 1; i < nums.size() - 1; i += 2) {
        swap(nums[i], nums[i + 1]);
    }
    return nums;
}

The algorithm starts by sorting the given integer array nums in ascending order. Then, iterates through the sorted array using a for-loop, swapping the numbers at the even index with the number immediately after it. The next step in the loop is done by incrementing the iterator by 2 (i.e., i += 2). The algorithm continues until the entire array is rearranged. Finally, the rearranged array is returned.