Check if Matrix Is X-Matrix

🏠 ⬅️ ➡️

A square matrix is said to be an X-Matrix if both of the following conditions hold:

  1. All the elements in the diagonals of the matrix are non-zero.
  2. All other elements are 0.

Given a 2D integer array grid of size n x n representing a square matrix, return true if grid is an X-Matrix. Otherwise, return false.

Example 1:

Input: grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]] Output: true Explanation: Refer to the diagram above. An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0. Thus, grid is an X-Matrix.

Example 2:

Input: grid = [[5,7,0],[0,3,1],[0,5,0]] Output: false Explanation: Refer to the diagram above. An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0. Thus, grid is not an X-Matrix.

Constraints:

  • n == grid.length == grid[i].length
  • 3 <= n <= 100
  • 0 <= grid[i][j] <= 105

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

    integer, parameter :: n = 4
    integer, dimension(n, n) :: grid
    logical :: is_x_matrix

    ! Example 1
    grid(:, :) = reshape([2, 0, 0, 1, &
                           0, 3, 1, 0, &
                           0, 5, 2, 0, &
                           4, 0, 0, 2], shape(grid))
    write(*, '(L1)') is_x_matrix(grid)

    ! Example 2
    grid(:, :) = reshape([5, 7, 0, &
                           0, 3, 1, &
                           0, 5, 0], shape(grid))
    write(*, '(L1)') is_x_matrix(grid)

contains

    function is_x_matrix(grid) result(is_x)
        implicit none
        integer, intent(in) :: grid(:, :)
        logical :: is_x
        integer :: i, j

        is_x = .true.

        do i = 1, size(grid, 1)
            if (grid(i, i) == 0) then
                is_x = .false.
                exit
            end if
        end do

        if (is_x) then
            do i = 1, size(grid, 1) - 1
                do j = i + 1, size(grid, 2)
                    if (grid(i, j) /= 0) then
                        is_x = .false.
                        exit
                    end if
                end do
                if (.not. is_x) exit
            end do
        end if

        if (is_x) then
            do j = 1, size(grid, 2)
                if (grid(j, j) == 0) then
                    is_x = .false.
                    exit
                end if
            end do
        end if

    end function is_x_matrix

end program main
Compiled
Executed
Correct
module x_matrix
implicit none
private
public :: is_x_matrix
interface
    function is_x_matrix_helper(grid, n) result(is_x_matrix)
        implicit none
        integer, intent(in) :: grid(:, :)
        integer, intent(in) :: n
        logical :: is_x_matrix
    end function is_x_matrix_helper
end interface
contains
function is_x_matrix(grid) result(is_x_matrix)
    implicit none
    integer, intent(in) :: grid(:, :)
    integer :: n
    logical :: is_x_matrix
    n = size(grid, 1)
    is_x_matrix = is_x_matrix_helper(grid, n)
end function is_x_matrix
function is_x_matrix_helper(grid, n) result(is_x_matrix)
    implicit none
    integer, intent(in) :: grid(:, :)
    integer, intent(in) :: n
    integer :: i, j
    logical :: is_x_matrix
    is_x_matrix = .true.
    do i = 1, n
        if (grid(i, i) == 0) then
            is_x_matrix = .false.
            exit
        end if
    end do
    if (is_x_matrix) then
        do i = 1, n
            do j = 1, n
                if (i /= j .and. grid(i, j) /= 0) then
                    is_x_matrix = .false.
                    exit
                end if
            end do
            if (.not. is_x_matrix) exit
        end do
    end if
end function is_x_matrix_helper
end module x_matrix

program test_x_matrix
    use x_matrix, only : is_x_matrix
    implicit none
    integer, parameter :: n = 4
    integer, parameter :: grid(n, n) = reshape([2, 0, 0, 1, 0, 3, 1, 0, 0, 5, 2, 0, 4, 0, 0, 2], [n, n])
    logical :: is_x_matrix_result
    is_x_matrix_result = is_x_matrix(grid)
    if (is_x_matrix_result) then
        print *, "The grid is an X-Matrix."
    else
        print *, "The grid is not an X-Matrix."
    end if
end program test_x_matrix
🌐 Data from online sources
def longest_repeat_substring(s, query_characters, query_indices):
    k = len(query_characters)
    lengths = []
    s_copy = list(s)

    for i in range(k):
        s_copy[query_indices[i]] = query_characters[i]
        max_len = cur_len = 1
        for j in range(1, len(s_copy)):
            if s_copy[j] == s_copy[j-1]:
                cur_len += 1
            else:
                max_len = max(max_len, cur_len)
                cur_len = 1
        max_len = max(max_len, cur_len)
        lengths.append(max_len)

    return lengths
  1. Initialize an empty list/array lengths to store the results.
  2. Create a copy of the string s, as we will modify it in each query.
  3. Iterate through each query using a for loop based on the length of queryCharacters. a. Update the character at the given index in sCopy using the current query. b. Initialize maxLen as the maximum length of substring found so far and curLen as the current length of the repeating substring. c. Iterate through the modified string and compare adjacent characters. i. If the characters are the same, increment curLen. ii. If the characters are different, update maxLen with the greater value between maxLen and curLen, and reset curLen to 1. d. After the loop ends, update maxLen with the greater value between maxLen and curLen. e. Append maxLen to the lengths list/array.
  4. Return the lengths list/array containing the length of the longest substring of sCopy consisting of only one repeating character after each query.
🌐 Data from online sources
#include <vector>
#include <string>
using namespace std;

vector<int> longestRepeatSubstring(const string& s, const string& queryCharacters, const vector<int>& queryIndices) {
    int k = queryCharacters.size();
    vector<int> lengths(k);
    string sCopy = s;

    for (int i = 0; i < k; ++i) {
        sCopy[queryIndices[i]] = queryCharacters[i];
        int maxLen = 0, curLen = 1;
        for (int j = 1; j < sCopy.size(); ++j) {
            if (sCopy[j] == sCopy[j-1]) {
                curLen++;
            } else {
                maxLen = max(maxLen, curLen);
                curLen = 1;
            }
        }
        maxLen = max(maxLen, curLen);
        lengths[i] = maxLen;
    }

    return lengths;
}
  1. Initialize an empty list/array lengths to store the results.
  2. Create a copy of the string s, as we will modify it in each query.
  3. Iterate through each query using a for loop based on the length of queryCharacters. a. Update the character at the given index in sCopy using the current query. b. Initialize maxLen as the maximum length of substring found so far and curLen as the current length of the repeating substring. c. Iterate through the modified string and compare adjacent characters. i. If the characters are the same, increment curLen. ii. If the characters are different, update maxLen with the greater value between maxLen and curLen, and reset curLen to 1. d. After the loop ends, update maxLen with the greater value between maxLen and curLen. e. Append maxLen to the lengths list/array.
  4. Return the lengths list/array containing the length of the longest substring of sCopy consisting of only one repeating character after each query.