[LeetCode] Valid Word Square

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).

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool validWordSquare(vector<string>& s) {
int n = s.size();
for(int i = 0; i < n; i++) {
if(s[i].size() > n) return false;
for(int j = 0; j < s[i].size(); j++) {
if(s[i][j] != s[j][i]) return false;
}
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/03/01/PS/LeetCode/valid-word-square/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.