Check if Every Row and Column Contains All Numbers

🏠 ⬅️ ➑️

An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).

Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.

Example 1:

Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.

Example 2:

Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 100
  • 1 <= matrix[i][j] <= n

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

    integer, parameter :: n = 3
    integer, dimension(n, n) :: matrix
    logical :: result

    ! Example 1
    matrix(:, :) = reshape([1, 2, 3, 3, 1, 2, 2, 3, 1], shape(matrix))
    write(*, '(L1)') is_valid(matrix)

    ! Example 2
    matrix(:, :) = reshape([1, 1, 1, 1, 2, 2, 2, 2, 3], shape(matrix))
    write(*, '(L1)') is_valid(matrix)

contains

    function is_valid(matrix) result(result)
        implicit none
        integer, intent(in) :: matrix(:, :)
        integer :: i, j
        logical :: result

        result = .true.

        ! Check rows
        do i = 1, size(matrix, 1)
            do j = 1, size(matrix, 2)
                if (count(matrix(i, :) == j) /= 1) then
                    result = .false.
                    exit
                end if
            end do
        end do

        ! Check columns
        do j = 1, size(matrix, 2)
            do i = 1, size(matrix, 1)
                if (count(matrix(:, j) == i) /= 1) then
                    result = .false.
                    exit
                end if
            end do
        end do
    end function is_valid
end program main
❌ Compiled
❌ Executed
❌ Correct
module is_valid_matrix

implicit none

contains

function is_valid_matrix(matrix) result(valid)

integer, intent(in) :: matrix(:, :)
logical :: valid

! Variables
integer :: n, i, j

! Initialize variables
n = size(matrix, 1)
valid = .true.

! Check if every row contains all the integers from 1 to n
do i = 1, n
    if (count(matrix(i, :) == i) /= n) then
        valid = .false.
        exit
    end if
end do

! Check if every column contains all the integers from 1 to n
do j = 1, n
    if (count(matrix(:, j) == j) /= n) then
        valid = .false.
        exit
    end if
end do

end function is_valid_matrix

end module is_valid_matrix

program test_is_valid_matrix

use is_valid_matrix

implicit none

! Variables
integer, allocatable :: matrix(:, :)
integer :: n
logical :: valid

! Test case 1
n = 3
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 3, 1, 2, 3, 2, 3], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
    write (*,*) "Test case 1: Pass"
else
    write (*,*) "Test case 1: Fail"
end if

! Test case 2
n = 3
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 2, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
    write (*,*) "Test case 2: Pass"
else
    write (*,*) "Test case 2: Fail"
end if

! Test case 3
n = 4
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
    write (*,*) "Test case 3: Pass"
else
    write (*,*) "Test case 3: Fail"
end if

! Test case 4
n = 4
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
    write (*,*) "Test case 4: Pass"
else
    write (*,*) "Test case 4: Fail"
end if

! Test case 5
n = 5
allocate(matrix(n, n))
matrix = reshape([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [n, n])
valid = is_valid_matrix(matrix)
if (valid) then
    write (*,*) "Test case 5: Pass"
else
    write (*,*) "Test case 5: Fail"
end if

! Test case 6
n = 5
allocate(matrix(n, n))
matrix = reshape([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], [n, n])
valid = is_valid_matrix(matrix)
if (.not. valid) then
    write (*,*) "Test case 6: Pass"
else
    write (*,*) "Test case 6: Fail"
end if

end program test_is_valid_matrix
🌐 Data from online sources
def count_pairs(nums, target):
    count = 0
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] + nums[j] == target or nums[j] + nums[i] == target:
                count += 1
    return count

The algorithm iterates through all possible pairs of indices i and j where i != j by using nested loops. For each pair, we check if the concatenation of nums[i] + nums[j] or nums[j] + nums[i] equals the target. If either condition is true, we increment the count. Finally, we return the count of such pairs. This solution has a complexity of O(n^2).

🌐 Data from online sources
#include <string>
#include <vector>

int countPairs(const std::vector<std::string>& nums, const std::string& target) {
    int count = 0;
    for (size_t i = 0; i < nums.size(); ++i) {
        for (size_t j = i + 1; j < nums.size(); ++j) {
            if (nums[i] + nums[j] == target || nums[j] + nums[i] == target) {
                ++count;
            }
        }
    }
    return count;
}

The algorithm iterates through all possible pairs of indices i and j where i != j by using nested loops. For each pair, we check if the concatenation of nums[i] + nums[j] or nums[j] + nums[i] equals the target. If either condition is true, we increment the count. Finally, we return the count of such pairs. This solution has a complexity of O(n^2).