lc422-Valid Word Square

Given a sequence of words, check whether it forms a valid word square.

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

Note:
The number of words given is at least 1 and does not exceed 500.
Word length will be at least 1 and does not exceed 500.
Each word contains only lowercase English alphabet a-z.

我有不审题和自动脑补题意的坏习惯,一开始以为输入就是一个square..= =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool validWordSquare(vector<string>& words) {
int m = words.size();
for (int i = 0; i < m; i++) {
int len = words[i].length();
if (len > m) return false;
for (int j = 0; j < len; j++) {
if (words[j].length() < i+1 || words[j][i] != words[i][j])
return false;
}
}
return true;
}
};