Skip to content

Commit b9206a8

Browse files
refactor 422
1 parent 3a546a3 commit b9206a8

File tree

2 files changed

+54
-59
lines changed

2 files changed

+54
-59
lines changed

src/main/java/com/fishercoder/solutions/_422.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.List;
44

55
/**
6+
* 422. Valid Word Square
7+
*
68
* Given a sequence of words, check whether it forms a valid word square.
79
810
A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).
@@ -71,22 +73,23 @@
7173
*/
7274
public class _422 {
7375

74-
public boolean validWordSquare(List<String> words) {
75-
for (int i = 0; i < words.size(); i++) {
76-
String word = words.get(i);
77-
for (int j = 0; j < word.length(); j++) {
78-
if (j >= words.size()) {
79-
return false;
80-
}
81-
if (i >= words.get(j).length()) {
82-
return false;
83-
}
84-
if (word.charAt(j) != words.get(j).charAt(i)) {
85-
return false;
76+
public static class Solution1 {
77+
public boolean validWordSquare(List<String> words) {
78+
for (int i = 0; i < words.size(); i++) {
79+
String word = words.get(i);
80+
for (int j = 0; j < word.length(); j++) {
81+
if (j >= words.size()) {
82+
return false;
83+
}
84+
if (i >= words.get(j).length()) {
85+
return false;
86+
}
87+
if (word.charAt(j) != words.get(j).charAt(i)) {
88+
return false;
89+
}
8690
}
8791
}
92+
return true;
8893
}
89-
return true;
9094
}
91-
9295
}

src/test/java/com/fishercoder/_422Test.java

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,41 @@
1212
import static junit.framework.Assert.assertEquals;
1313

1414
public class _422Test {
15-
private static _422 test;
16-
private static boolean expected;
17-
private static boolean actual;
18-
private static List<String> words;
19-
20-
@BeforeClass
21-
public static void setup() {
22-
test = new _422();
23-
}
24-
25-
@Before
26-
public void setupForEachTest() {
27-
}
28-
29-
@Test
30-
public void test1() {
31-
words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crmy", "dtye"));
32-
expected = true;
33-
actual = test.validWordSquare(words);
34-
assertEquals(expected, actual);
35-
}
36-
37-
@Test
38-
public void test2() {
39-
// abcd
40-
// bnrt
41-
// crm
42-
// dt
43-
words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crm", "dt"));
44-
expected = true;
45-
actual = test.validWordSquare(words);
46-
assertEquals(expected, actual);
47-
}
48-
49-
@Test
50-
public void test3() {
51-
//ball
52-
//asee
53-
//let
54-
//lep
55-
words = new ArrayList<>(Arrays.asList("ball", "asee", "let", "lep"));
56-
expected = false;
57-
actual = test.validWordSquare(words);
58-
assertEquals(expected, actual);
59-
}
15+
private static _422.Solution1 test;
16+
private static boolean expected;
17+
private static boolean actual;
18+
private static List<String> words;
19+
20+
@BeforeClass
21+
public static void setup() {
22+
test = new _422.Solution1();
23+
}
24+
25+
@Before
26+
public void setupForEachTest() {
27+
}
28+
29+
@Test
30+
public void test1() {
31+
words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crmy", "dtye"));
32+
expected = true;
33+
actual = test.validWordSquare(words);
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
public void test2() {
39+
words = new ArrayList<>(Arrays.asList("abcd", "bnrt", "crm", "dt"));
40+
expected = true;
41+
actual = test.validWordSquare(words);
42+
assertEquals(expected, actual);
43+
}
44+
45+
@Test
46+
public void test3() {
47+
words = new ArrayList<>(Arrays.asList("ball", "asee", "let", "lep"));
48+
expected = false;
49+
actual = test.validWordSquare(words);
50+
assertEquals(expected, actual);
51+
}
6052
}

0 commit comments

Comments
 (0)