1 pass simple C++ solution


  • 0
    X
    class Solution {
    public:
        bool validWordSquare(vector<string>& words) {
            int n_row = words.size();
            for (size_t row = 0; row < n_row; row++) {
                int n_col = words[row].size();
                if (n_col > n_row) return false;
                size_t col = row + 1;
                for (;col < n_row; col++) {
                    if (col < n_col) {
                        if (words[row][col] != words[col][row]) return false;
                    } else {
                        if (words[col].size() > row) return false;
                    }
                }
            } 
            return true;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.