A square matrix is said to be an X-Matrix if both of the following conditions hold:
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
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
temp.f95:23:25: 23 | function is_x_matrix(grid) result(is_x) | 1 Error: Procedure ‘is_x_matrix’ at (1) has an explicit interface from a previous declaration temp.f95:24:21: 24 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:25:41: 25 | integer, intent(in) :: grid(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:26:23: 26 | logical :: is_x | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:23: 27 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:29:21: 29 | is_x = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:31: 31 | do i = 1, size(grid, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:32:37: 32 | if (grid(i, i) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:33:30: 33 | is_x = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:20: 34 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:35:15: 35 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:36:11: 36 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:22: 38 | if (is_x) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:39:39: 39 | do i = 1, size(grid, 1) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:40:43: 40 | do j = i + 1, size(grid, 2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:41:45: 41 | if (grid(i, j) /= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:42:38: 42 | is_x = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:43:28: 43 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:44:23: 44 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:45:19: 45 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:46:36: 46 | if (.not. is_x) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:47:15: 47 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:48:11: 48 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:50:22: 50 | if (is_x) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:51:35: 51 | do j = 1, size(grid, 2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:52:41: 52 | if (grid(j, j) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:53:34: 53 | is_x = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:54:24: 54 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:55:19: 55 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:56:15: 56 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:57:11: 57 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:59:7: 59 | end function is_x_matrix | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:16:25: 16 | grid(:, :) = reshape([5, 7, 0, & | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
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
temp.f95:14:46: 14 | function is_x_matrix(grid) result(is_x_matrix) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:15:17: 15 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:16:37: 16 | integer, intent(in) :: grid(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:17:16: 17 | integer :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:18:26: 18 | logical :: is_x_matrix | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:19:21: 19 | n = size(grid, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:16: 20 | is_x_matrix = is_x_matrix_helper(grid, n) | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:21:3: 21 | end function is_x_matrix | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:28: 6 | function is_x_matrix_helper(grid, n) result(is_x_matrix) | 2 ...... 22 | function is_x_matrix_helper(grid, n) result(is_x_matrix) | 1 Error: Procedure ‘is_x_matrix_helper’ at (1) is already defined at (2) temp.f95:23:17: 23 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:24:37: 24 | integer, intent(in) :: grid(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:28: 25 | integer, intent(in) :: n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:26:19: 26 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:26: 27 | logical :: is_x_matrix | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:28:16: 28 | is_x_matrix = .true. | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:29:15: 29 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:30:33: 30 | if (grid(i, i) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:31:24: 31 | is_x_matrix = .false. | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:32:16: 32 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:33:11: 33 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:7: 34 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:25: 35 | if (is_x_matrix) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:36:19: 36 | do i = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:37:23: 37 | do j = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:38:54: 38 | if (i /= j .and. grid(i, j) /= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:39:32: 39 | is_x_matrix = .false. | 1 Error: Symbol ‘is_x_matrix’ at (1) has already been host associated temp.f95:40:24: 40 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:41:19: 41 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:42:15: 42 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:39: 43 | if (.not. is_x_matrix) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:44:11: 44 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:7: 45 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:46:3: 46 | end function is_x_matrix_helper | 1 Error: Expecting END MODULE statement at (1) temp.f95:4:21: 4 | public :: is_x_matrix | 1 Error: Symbol ‘is_x_matrix’ at (1) has no IMPLICIT type temp.f95:50:9: 50 | use x_matrix, only : is_x_matrix | 1 Fatal Error: Cannot open module file ‘x_matrix.mod’ for reading at (1): No such file or directory compilation terminated.
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
lengths
to store the results.s
, as we will modify it in each query.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.lengths
list/array containing the length of the longest substring of sCopy
consisting of only one repeating character after each query.#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;
}
lengths
to store the results.s
, as we will modify it in each query.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.lengths
list/array containing the length of the longest substring of sCopy
consisting of only one repeating character after each query.