Skip to content

Commit 2446f4b

Browse files
authored
Update Valid Word Square.java
1 parent 78dad25 commit 2446f4b

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

Easy/Valid Word Square.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
class Solution {
2-
public boolean validWordSquare(List<String> words) {
3-
return IntStream.range(0, words.size())
4-
.allMatch(i -> words.get(i).equals(verticalRepresentation(words, i)));
5-
}
6-
7-
private String verticalRepresentation(List<String> words, int col){
8-
StringBuilder sb = new StringBuilder();
9-
for (String word : words) {
10-
if (col < word.length()) {
11-
sb.append(word.charAt(col));
12-
}
2+
public boolean validWordSquare(List<String> words) {
3+
int n = words.size();
4+
for (int i = 0; i < n; i++){
5+
for (int j = 0; j < words.get(i).length(); j++){
6+
if(j >= n || words.get(j).length() <= i || words.get(j).charAt(i) != words.get(i).charAt(j)) {
7+
return false;
8+
}
9+
}
10+
}
11+
return true;
1312
}
14-
return sb.toString();
15-
}
1613
}

0 commit comments

Comments
 (0)