A simple and efficient way to check Toeplitz matrix which always has the same row and column along its main diagonal

2023-06-30 16:30:19
How to check if a special Toeplitz matrix, which has the same row and column along its main diagonal, is symmetric along its main diagonal using a simple and efficient method?

Toeplitz Matrix

Chinese name: 托普利兹矩阵

Example

422. Valid Word Square

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.

Answer

Traverse along main diagonal

var validWordSquare = function(words) {
  const n = words.length
  for (let i = 0; i < n; i++) {
    let cnt = 0
    for (let j = i + 1; j < n; j++) {
      if (words[j].length > i) cnt++
      else break
    }
    const m = words[i].length
    if (cnt != Math.max(m - i - 1, 0)) return false
    for (let j = i + 1; j < m; j++) {
      if (words[i][j] !== words[j][i]) return false
    }
  }
  return true
};
func validWordSquare(words []string) bool {
  n := len(words)
  for i := 0; i < n; i++ {
    cnt := 0
    for j := i + 1; j < n; j++ {
      if len(words[j]) > i {
        cnt++
      } else {
        break
      }
    }
    m := len(words[i])
    if cnt != max(0, m - i - 1) {
      return false
    }
    for j := i + 1; j < m; j++ {
      if words[i][j] != words[j][i] {
        return false
      }
    }
  }
  return true
}
func max(a, b int) int {
  if a > b {
    return a
  }
  return b
}
class Solution {
  function validWordSquare($words) {
    $n = count($words);
    for ($i = 0; $i < $n; $i++) {
      $cnt = 0;
      for ($j = $i + 1; $j < $n; $j++) {
        if (strlen($words[$j]) > $i) $cnt++;
        else break;
      }
      $m = strlen($words[$i]);
      if ($cnt != max(0, $m - $i - 1)) return false;
      for ($j = $i + 1; $j < $m; $j++) {
        if ($words[$i][$j] != $words[$j][$i]) return false;
      }
    }
    return true;
  }
}
class Solution {
  public boolean validWordSquare(List<String> words) {
    int n = words.size();
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = i + 1; j < n; j++) {
        if (words.get(j).length() > i) cnt++;
        else break;
      }
      int m = words.get(i).length();
      if (cnt != Math.max(m - i - 1, 0)) return false;
      for (int j = i + 1; j < m; j++) {
        char a = words.get(i).charAt(j);
        char b = words.get(j).charAt(i);
        if (a != b) return false;
      }
    }
    return true;
  }
}
public class Solution {
  public bool ValidWordSquare(IList<string> words) {
    int n = words.Count;
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = i + 1; j < n; j++) {
        if (words[j].Length > i) cnt++;
        else break;
      }
      int m = words[i].Length;
      if (cnt != Math.Max(0, m - i - 1)) return false;
      for (int j = i + 1; j < m; j++) {
        if (words[i][j] != words[j][i]) return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool validWordSquare(vector<string>& words) {
    int n = words.size();
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = i + 1; j < n; j++) {
        if (words[j].size() > i) cnt++;
        else break;
      }
      int m = words[i].size();
      if (cnt != max(0, m - i - 1)) return false;
      for (int j = i + 1; j < m; j++) {
        if (words[i][j] != words[j][i]) return false;
      }
    }
    return true;
  }
};
#define MAX(a, b) (a > b ? a : b)
bool validWordSquare(char ** words, int wordsSize){
  for (int i = 0; i < wordsSize; i++) {
    int cnt = 0;
    for (int j = i + 1; j < wordsSize; j++) {
      if (strlen(words[j]) > i) cnt++;
      else break;
    }
    int m = strlen(words[i]);
    if (cnt != MAX(0, m - i - 1)) return false;
    for (int j = i + 1; j < m; j++) {
      if (words[i][j] != words[j][i]) return false;
    }
  }
  return true;
}
class Solution:
  def validWordSquare(self, words: List[str]) -> bool:
    n = len(words)
    for i in range(0, n):
      cnt = 0
      for j in range(i + 1, n):
        if len(words[j]) > i: cnt += 1
        else: break
      m = len(words[i])
      if cnt != max(0, m - i - 1): return False
      for j in range(i + 1, m):
        if words[i][j] != words[j][i]: return False
    return True

Dynamic Programming Algorithm with Space Compression Optimization and different ways to shallow copy a array list
Using dynamic programming algorithm with space compression optimization and different ways to shallow copy a Array List, solving the "931. Minimum Falling Path Sum"
顺序遍历或倒序遍历原地修改,单变量或双变量记忆第 0 行或第 0 列是否存在 0。3 解法求解《面试题 01.08. 零矩阵》
顺序遍历,双变量记忆第 0 行和第 0 列是否存在 0。倒序遍历,单变量记忆第 0 列是否存在 0。倒序遍历,单变量记忆第 0 行是否存在 0。3 解法求解《面试题 01.08. 零矩阵》
定长列表(数组),原地修改,2 解法求解《1582. 二进制矩阵中的特殊位置》
定长列表(数组),原地修改,Python 使用 zip 旋转矩阵,2 解法求解《1582. 二进制矩阵中的特殊位置》