C++ brutal force


  • 0
    B

    no trick, just check row = col

    class Solution {
    public:
        bool validWordSquare(vector<string>& words) {
            for(int r = 0; r < words.size(); r++)
            {
                for(int c = 0; c < words[r].size(); c++)
                {
                    //check c and r exist and letter is the same
                    if(words.size() <= c) return false;
                    if(words[c].length() <= r) return false;
                    if(words[c][r] != words[r][c]) return false;
                }
            }
            return true;
        }
    };

Log in to reply
 

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