5 line code AC solution, easy understand, beat 88%


  • 0
    I

    "words[i][j]" index is always valid
    Only need to check index of words[j][i]. j should not be equal or greater than number of words; i should not be equal or greater than the length of words[j].

     public bool ValidWordSquare(IList<string> words) {
            for(var i= 0; i < words.Count; i++)
            {
                for(var j = 0; j < words[i].Length; j++)
                   if(j >= words.Count || i >= words[j].Length || words[i][j]!=words[j][i])
                      return false;
            }
            return true;
        }
    

Log in to reply
 

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