Given an n x n square matrix of words, check if it is a valid word square. That is, the kth row and column read the exact same string, where 0 ≤ k < n.
For example, the word square
["ball",
"area",
"lead",
"lady"]
is a valid word square because each word reads the same both horizontally and vertically.
Example 1:
Input: words = ["ball","area","lead","lady"]
Output: true
Explanation:
The 1st row and 1st column both read "ball".
The 2nd row and 2nd column both read "area".
The 3rd row and 3rd column both read "lead".
The 4th row and 4th column both read "lady".
Example 2:
Input: words = ["abat","baba","atan","atal"]
Output: false
Explanation:
The 1st row and 1st column both read "abat".
The 2nd row and 2nd column both read "baba".
The 3rd row and 3rd column read "atan" != "tana".
The 4th row and 4th column read "atal" != "lata".
Constraints:
1 <= words.length <= 500words[i].length <= 5001 <= words[i].length <= 500words[i] consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy to determine if a list of words forms a valid word square is about checking *every* single possibility. We want to see if the word at row 'r' is the same as the word at column 'r'. If we examine every word in this way, we can definitively say if the arrangement is a valid square.
Here's how the algorithm would work step-by-step:
def is_valid_word_square(words):
number_of_words = len(words)
for row_index in range(number_of_words):
word = words[row_index]
word_length = len(word)
# Iterate through each letter in the current word
for column_index in range(word_length):
# Check if the column index is within the bounds of other words
if column_index >= number_of_words:
return False
other_word = words[column_index]
# Check if the row index is within the bounds of the other word
if row_index >= len(other_word):
return False
# Compare the letter at (row, col) with the letter at (col, row)
if word[column_index] != other_word[row_index]:
return False
# If all checks pass, it's a valid word square
return TrueThe core idea is to check if the word arrangement is symmetric. We simply compare words in the rows to the words in the columns to ensure they match, indicating a valid word square.
Here's how the algorithm would work step-by-step:
def is_valid_word_square(words):
word_count = len(words)
for row_index in range(word_count):
word_length = len(words[row_index])
#Check if dimensions are not square
if word_length != word_count:
return False
for column_index in range(word_length):
# Check for out-of-bounds access
if column_index >= word_count or row_index >= len(words[column_index]):
return False
#This ensures we don't compare past the length of the column word
if column_index >= len(words[row_index]) or row_index >= len(words[column_index]):
return False
#Crucial step: Compare row and column chars for symmetry
if words[row_index][column_index] != words[column_index][row_index]:
return False
return True| Case | How to Handle |
|---|---|
| Null or empty input array | Return true immediately as an empty square is considered valid. |
| Array with only one string | Check if the single string's length is 1 and if the string's character at index 0 matches itself. |
| Array with strings of varying lengths | Return false because a valid word square requires all strings to be of the same length. |
| String contains non-alphabetic characters | The solution should handle non-alphabetic characters, either by throwing an error or ignoring them depending on requirements. |
| Input forms a square but fails validation in the middle | The validation should continue until the failure condition is identified, then return false. |
| Strings with different casing | Convert all strings to either lowercase or uppercase before processing to ensure case-insensitive validation. |
| Very long strings that might cause performance issues | The solution has O(n^2) complexity where n is the square size, so long strings won't cause issues, just longer execution time. |
| Valid square with identical characters | The solution correctly handles this by comparing characters at corresponding indices. |