返回介绍

solution / 0400-0499 / 0422.Valid Word Square / README_EN

发布于 2024-06-17 01:04:00 字数 5265 浏览 0 评论 0 收藏 0

422. Valid Word Square

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def validWordSquare(self, words: List[str]) -> bool:
    m = len(words)
    n = max(len(w) for w in words)
    if m != n:
      return False
    for j in range(n):
      if words[j] != "".join(w[j] for w in words if j < len(w)):
        return False
    return True
class Solution {
  public boolean validWordSquare(List<String> words) {
    int m = words.size();
    for (int i = 0; i < m; ++i) {
      int n = words.get(i).length();
      for (int j = 0; j < n; ++j) {
        if (j >= m || i >= words.get(j).length()) {
          return false;
        }
        if (words.get(i).charAt(j) != words.get(j).charAt(i)) {
          return false;
        }
      }
    }
    return true;
  }
}
class Solution {
public:
  bool validWordSquare(vector<string>& words) {
    int m = words.size();
    for (int i = 0; i < m; ++i) {
      int n = words[i].size();
      for (int j = 0; j < n; ++j) {
        if (j >= m || i >= words[j].size() || words[i][j] != words[j][i]) {
          return false;
        }
      }
    }
    return true;
  }
};
func validWordSquare(words []string) bool {
  m := len(words)
  for i, w := range words {
    for j := range w {
      if j >= m || i >= len(words[j]) || w[j] != words[j][i] {
        return false
      }
    }
  }
  return true
}
function validWordSquare(words: string[]): boolean {
  const m = words.length;
  for (let i = 0; i < m; ++i) {
    const n = words[i].length;
    for (let j = 0; j < n; ++j) {
      if (j >= m || i >= words[j].length || words[i][j] !== words[j][i]) {
        return false;
      }
    }
  }
  return true;
}

Solution 2

class Solution:
  def validWordSquare(self, words: List[str]) -> bool:
    m = len(words)
    for i, w in enumerate(words):
      for j, c in enumerate(w):
        if j >= m or i >= len(words[j]) or c != words[j][i]:
          return False
    return True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文