Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding all valid word squares in JavaScript
What is a Word Square?
A word square consists of a set of words written out in a square grid, such that the same words can be read both horizontally and vertically. This means the character at position (i, j) must equal the character at position (j, i).
For instance, one valid word square is:
H E A R T E M B E R A B U S E R E S I N T R E N D
We need to write a JavaScript function that takes an array of words and returns true if they form a valid word square, false otherwise.
Problem Analysis
For a word square to be valid:
- All words must have the same length
- The number of words must equal the length of each word (forming a square)
- Character at position (i, j) must equal character at position (j, i)
Example Input and Output
If the input word array is:
const arr = [ "abcd", "bnrt", "crmy", "dtye" ];
Then the output should be:
true
This forms a valid 4x4 word square where reading horizontally and vertically gives the same result.
Implementation
const arr1 = [
"abcd",
"bnrt",
"crmy",
"dtye"
];
const arr2 = [
"abcd",
"bnrt",
"crm",
"dt"
];
const findValidSquares = (arr = []) => {
// Check if array is empty
if (arr.length === 0) return true;
const n = arr.length;
// Check if all words have the same length as array length
for (let i = 0; i < n; i++) {
if (arr[i].length !== n) {
return false;
}
}
// Check if character at (i,j) equals character at (j,i)
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (arr[i][j] !== arr[j][i]) {
return false;
}
}
}
return true;
};
console.log("Valid square:", findValidSquares(arr1));
console.log("Invalid square:", findValidSquares(arr2));
Valid square: true Invalid square: false
How It Works
The algorithm works in three steps:
- Empty Check: Returns true for empty arrays (edge case)
- Dimension Check: Ensures all words have the same length as the array length, forming a square
- Symmetry Check: Verifies that arr[i][j] equals arr[j][i] for all positions
Alternative Approach with Validation
const validateWordSquare = (words) => {
if (!Array.isArray(words) || words.length === 0) {
return true;
}
const size = words.length;
// Validate dimensions and check symmetry in one pass
for (let i = 0; i < size; i++) {
if (typeof words[i] !== 'string' || words[i].length !== size) {
return false;
}
for (let j = 0; j < size; j++) {
if (words[i][j] !== words[j][i]) {
return false;
}
}
}
return true;
};
const testCases = [
["ball", "area", "lead", "lady"], // valid
["abc", "def", "ghi"], // invalid - not square pattern
[] // valid - empty
];
testCases.forEach((test, index) => {
console.log(`Test ${index + 1}:`, validateWordSquare(test));
});
Test 1: true Test 2: false Test 3: true
Conclusion
A valid word square requires equal dimensions and symmetric character placement where arr[i][j] equals arr[j][i]. The solution checks both structural validity and character symmetry efficiently in O(n²) time complexity.
