Valid Word Square - Problem
Given an array of strings words, return true if it forms a valid word square.
A sequence of strings forms a valid word square if the kth row and column read the same string, where 0 <= k < max(numRows, numColumns).
In other words, for each position (i, j), the character at words[i][j] should equal the character at words[j][i] (if both positions exist).
Input & Output
Example 1 — Valid Word Square
$
Input:
words = ["abcd","bnrt","crmy","dtye"]
›
Output:
true
💡 Note:
Row 0: "abcd" matches Column 0: "abcd", Row 1: "bnrt" matches Column 1: "bnrt", etc. All rows equal their corresponding columns.
Example 2 — Invalid Word Square
$
Input:
words = ["abcd","bnrt","crm","dt"]
›
Output:
false
💡 Note:
Row 2 is "crm" but Column 2 would be "mrd" (reading vertically). They don't match, so it's not a valid word square.
Example 3 — Single Word
$
Input:
words = ["a"]
›
Output:
true
💡 Note:
Single character forms a valid 1×1 word square where row 0 equals column 0.
Constraints
- 1 ≤ words.length ≤ 500
- 1 ≤ words[i].length ≤ 500
- words[i] consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of strings representing potential word square
2
Process
Check if each row equals its corresponding column
3
Output
Return true if valid, false otherwise
Key Takeaway
🎯 Key Insight: A word square is valid when it's symmetric - each row must equal its corresponding column
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code