4-line straightforward C++ solution


  • 0

    Just straightforward comparison of char words[j][i] == words[i][j], but the trick is to check range first.

        bool validWordSquare(vector<string>& words) {
          for (int i = 0; i < words.size();    ++i)
          for (int j = 0; j < words[i].size(); ++j)
            if (j >= words.size() || words[j].size() <= i || words[j][i] != words[i][j]) return false;
          return true;
        }
    

Log in to reply
 

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