Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-274

Valid Word Square

Problem

Return True if the list of words forms a word square, meaning the k-th row and the k-th column contain the same sequence of characters wherever both positions exist.

Compare words[i][j] with words[j][i] and fail on any mismatch or out-of-range access. Empty input is trivially valid.

Hint

A position outside the bounds of the transposed word breaks the square.

Test cases

case 1
in: [["abcd","bnrt","crmy","dtye"]]
expected: true
case 2
in: [["ab","ba"]]
expected: true
case 3
in: [["ball","area","read","lady"]]
expected: false
case 4
in: [["a"]]
expected: true
case 5
in: [[]]
expected: true
case 6
in: [["abc","b"]]
expected: false
Starter codePython
def valid_word_square(words):
    # Your code here
    pass

6 test cases · run your code against them in the editor