Given an array of strings words, return true if it forms a valid word square.

A sequence of strings forms a valid word square if the kth row and column read the same string, where 0 <= k < max(numRows, numColumns).

Example 1:

Input: words = [ "abcd ", "bnrt ", "crmy ", "dtye "] Output: true Explanation: The 1st row and 1st column both read "abcd ". The 2nd row and 2nd column both read "bnrt ". The 3rd row and 3rd column both read "crmy ". The 4th row and 4th column both read "dtye ". Therefore, it is a valid word square.

Example 2:

Input: words = [ "abcd ", "bnrt ", "crm ", "dt "] Output: true Explanation: The 1st row and 1st column both read "abcd ". The 2nd row and 2nd column both read "bnrt ". The 3rd row and 3rd column both read "crm ". The 4th row and 4th column both read "dt ". Therefore, it is a valid word square.

Example 3:

Input: words = [ "ball ", "area ", "read ", "lady "] Output: false Explanation: The 3rd row reads "read " while the 3rd column reads "lead ". Therefore, it is NOT a valid word square.

Constraints:

  • 1 <= words.length <= 500
  • 1 <= words[i].length <= 500
  • words[i] consists of only lowercase English letters.

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

    integer :: i, j, n
    character(len=500) :: words(500)
    logical :: valid_word_square

    ! Example 1
    words(1) = 'abcd '
    words(2) = 'bnrt '
    words(3) = 'crmy '
    words(4) = 'dtye '
    call assert(valid_word_square(words))

    ! Example 2
    words(1) = 'abcd '
    words(2) = 'bnrt '
    words(3) = 'crm '
    words(4) = 'dt '
    call assert(valid_word_square(words))

    ! Example 3
    words(1) = 'ball '
    words(2) = 'area '
    words(3) = 'read '
    words(4) = 'lady '
    call assert(.not. valid_word_square(words))

contains

    subroutine assert(condition)
        implicit none
        logical, intent(in) :: condition

        if (.not. condition) then
            write (*,*) 'Assertion failed!'
            stop 1
        end if
    end subroutine assert

    function valid_word_square(words) result(valid)
        implicit none
        character(len=*), intent(in) :: words(:)
        integer :: num_rows, num_cols, i, j
        logical :: valid

        num_rows = size(words)
        num_cols = size(words, dim=2)

        do i = 0, min(num_rows, num_cols) - 1
            do j = 0, min(num_rows, num_cols) - 1
                if (words(i+1)(j+1:j+1) /= words(j+1)(i+1:i+1)) then
                    valid = .false.
                    exit
                end if
            end do
            if (.not. valid) exit
        end do
    end function valid_word_square
end program main
❌ Compiled
❌ Executed
❌ Correct
program is_word_square
      implicit none

      integer, parameter :: MAX_LENGTH = 500

      character(len=MAX_LENGTH) :: words(MAX_LENGTH)
      integer :: num_rows, num_cols, i, j
      logical :: is_valid

      read(*,*) num_rows, num_cols
      read(*,*) words

      is_valid = .true.

      ! Check rows
      do i = 1, num_rows
         do j = 1, num_cols
            if (words(i)(j:j) /= words(i)(1:1)) then
               is_valid = .false.
               exit
            end if
         end do
         if (.not. is_valid) exit
      end do

      ! Check columns
      do j = 1, num_cols
         do i = 1, num_rows
            if (words(i)(j:j) /= words(1)(j:j)) then
               is_valid = .false.
               exit
            end if
         end do
         if (.not. is_valid) exit
      end do

      if (is_valid) then
         print *, "True"
      else
         print *, "False"
      end if

      end program is_word_square

This program uses a 2D array to store the input words. It then checks each row and column to ensure that the characters in each position are the same. If any of the checks fail, the program sets `is_valid` to `.false.` and exits the loop. If all checks pass, the program sets `is_valid` to `.true.` and prints "True" to stdout.

Note that the program uses a parameter `MAX_LENGTH` to set the maximum length of each word. This is necessary because the input words can have any length, but the program needs to know how much memory to allocate for the 2D array.

Also note that the program uses the `implicit none` statement to prevent the use of any uninitialized variables. This is a good practice to follow in Fortran programs to avoid errors and improve code readability.

This program should run with all provided examples and output "True" or "False" to stdout, depending on whether the input array forms a valid word square.
🌐 Data from online sources
def valid_word_square(words):
    for i in range(len(words)):
        for j in range(len(words[i])):
            if j >= len(words) or i >= len(words[j]) or words[i][j] != words[j][i]:
                return False
    return True
This solution is based on validating the elements of an array in place. Loop through each row `i` and column `j` of the given array. In the inner loop, check if the condition `j >= words.size() || i >= words[j].size() || words[i][j] != words[j][i]` is true. If the condition is true, that means it's not a valid word square, so return false. If the loop completes without returning false, then it's a valid word square, so return true.
🌐 Data from online sources
bool validWordSquare(vector<string>& words) {
    for (int i = 0; i < words.size(); ++i) {
        for (int j = 0; j < words[i].size(); ++j) {
            if (j >= words.size() || i >= words[j].size() || words[i][j] != words[j][i]) {
                return false;
            }
        }
    }
    return true;
}
This solution is based on validating the elements of an array in place. Loop through each row `i` and column `j` of the given array. In the inner loop, check if the condition `j >= words.size() || i >= words[j].size() || words[i][j] != words[j][i]` is true. If the condition is true, that means it's not a valid word square, so return false. If the loop completes without returning false, then it's a valid word square, so return true.