618. 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 kth column read the same string, where 0 <= k < max(numRows, numColumns).

Example 1:

Input: words = ["mote", "oval", "tele", "else"]

Output: false

Explanation:

The 2nd row reads "oval", while the 2nd column reads "otel". Thus, it is not a valid word square.

Example 2:

Input: words = ["flow", "lore", "oven", "west"]

Output: false

Explanation:

The 3rd row reads "oven", while the 3rd column reads "lven". Thus, it is not a valid word square.

Now Your Turn!

Pick the correct output for the given input

Input: words = ["code", "open", "deep", "echo"]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool validWordSquare(vector<string>& words) {
// Your code goes here
}
};
Test Case

Input:

Words