Leetcode 422 Solution

This article provides solution to leetcode question 422 (valid-word-square)

https://leetcode.com/problems/valid-word-square

Solution

class Solution {
public:
    bool validWordSquare(vector<string>& words) {
        int m = words.size();
        for (int i = 0; i < m; i++)
        {
            auto& word = words[i];
            for (int j = 0; j < word.size(); j++)
            {
                if (i == j)
                    continue;

                if (j >= m || i >= words[j].size())
                    return false;

                if (words[i][j] != words[j][i])
                    return false;
            }
        }

        return true;
    }
};