Skip to content

Commit 592944f

Browse files
committed
Add solution #422
1 parent c35d51b commit 592944f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@
410410
419|[Battleships in a Board](./solutions/0419-battleships-in-a-board.js)|Medium|
411411
420|[Strong Password Checker](./solutions/0420-strong-password-checker.js)|Hard|
412412
421|[Maximum XOR of Two Numbers in an Array](./solutions/0421-maximum-xor-of-two-numbers-in-an-array.js)|Medium|
413+
422|[Valid Word Square](./solutions/0422-valid-word-square.js)|Easy|
413414
423|[Reconstruct Original Digits from English](./solutions/0423-reconstruct-original-digits-from-english.js)|Medium|
414415
424|[Longest Repeating Character Replacement](./solutions/0424-longest-repeating-character-replacement.js)|Medium|
415416
427|[Construct Quad Tree](./solutions/0427-construct-quad-tree.js)|Medium|

solutions/0422-valid-word-square.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 422. Valid Word Square
3+
* https://leetcode.com/problems/valid-word-square/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of strings words, return true if it forms a valid word square.
7+
*
8+
* A sequence of strings forms a valid word square if the kth row and column read the same
9+
* string, where 0 <= k < max(numRows, numColumns).
10+
*/
11+
12+
/**
13+
* @param {string[]} words
14+
* @return {boolean}
15+
*/
16+
var validWordSquare = function(words) {
17+
const rowCount = words.length;
18+
19+
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
20+
const row = words[rowIndex];
21+
for (let colIndex = 0; colIndex < row.length; colIndex++) {
22+
if (colIndex >= rowCount || rowIndex >= words[colIndex].length
23+
|| row[colIndex] !== words[colIndex][rowIndex]) {
24+
return false;
25+
}
26+
}
27+
}
28+
29+
return true;
30+
};

0 commit comments

Comments
 (0)