# 0422. Valid Word Square

<https://leetcode.com/problems/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:**

![](https://assets.leetcode.com/uploads/2021/04/09/validsq1-grid.jpg)

```
**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:**

![](https://assets.leetcode.com/uploads/2021/04/09/validsq2-grid.jpg)

```
**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:**

![](https://assets.leetcode.com/uploads/2021/04/09/validsq3-grid.jpg)

```
**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.

## ac

read carefully

```java
class Solution {
    public boolean validWordSquare(List<String> words) {
        // edge cases
        if (words == null || words.size() == 0 || words.size() != words.get(0).length()) return false;

        for (int i = 0; i < words.size(); i++) {
            String ver = readVertical(words, i);
            if (!words.get(i).equals(ver)) return false;
        }

        return true;
    }

    private String readVertical(List<String> words, int col) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < words.size(); i++) {
            if (col < words.get(i).length()) 
                sb.append(words.get(i).charAt(col));
        }

        return sb.toString();
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/0422-valid-word-square.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
