Skip to content

Commit 02d94cc

Browse files
refactor 127
1 parent 8a9a355 commit 02d94cc

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,47 @@
3131
*/
3232

3333
public class _127 {
34+
public static class Solution1 {
3435

35-
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
36-
Set<String> beginSet = new HashSet<>();
37-
Set<String> endSet = new HashSet<>();
38-
Set<String> visited = new HashSet<>();
39-
Set<String> dict = new HashSet<>(wordList);
40-
int len = 1;
36+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
37+
Set<String> beginSet = new HashSet<>();
38+
Set<String> endSet = new HashSet<>();
39+
Set<String> visited = new HashSet<>();
40+
Set<String> dict = new HashSet<>(wordList);
41+
int len = 1;
4142

42-
beginSet.add(beginWord);
43+
beginSet.add(beginWord);
4344

44-
if (dict.contains(endWord)) {
45-
endSet.add(endWord);
46-
}
45+
if (dict.contains(endWord)) {
46+
endSet.add(endWord);
47+
}
4748

48-
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
49-
Set<String> nextBeginSet = new HashSet<>();
50-
for (String word : beginSet) {
51-
char[] chars = word.toCharArray();
52-
for (int i = 0; i < chars.length; i++) {
53-
for (char c = 'a'; c <= 'z'; c++) {
54-
char old = chars[i];
55-
chars[i] = c;
56-
String newWord = new String(chars);
57-
if (endSet.contains(newWord)) {
58-
return len + 1;
59-
}
49+
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
50+
Set<String> nextBeginSet = new HashSet<>();
51+
for (String word : beginSet) {
52+
char[] chars = word.toCharArray();
53+
for (int i = 0; i < chars.length; i++) {
54+
for (char c = 'a'; c <= 'z'; c++) {
55+
char old = chars[i];
56+
chars[i] = c;
57+
String newWord = new String(chars);
58+
if (endSet.contains(newWord)) {
59+
return len + 1;
60+
}
6061

61-
if (!visited.contains(newWord) && dict.contains(newWord)) {
62-
visited.add(newWord);
63-
nextBeginSet.add(newWord);
62+
if (!visited.contains(newWord) && dict.contains(newWord)) {
63+
visited.add(newWord);
64+
nextBeginSet.add(newWord);
65+
}
66+
chars[i] = old;
6467
}
65-
chars[i] = old;
6668
}
6769
}
68-
}
6970

70-
beginSet = nextBeginSet;
71-
len++;
71+
beginSet = nextBeginSet;
72+
len++;
73+
}
74+
return 0;
7275
}
73-
return 0;
7476
}
7577
}

src/test/java/com/fishercoder/_127Test.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,24 @@
1010

1111
import static org.junit.Assert.assertEquals;
1212

13-
/**
14-
* Created by stevesun on 6/5/17.
15-
*/
1613
public class _127Test {
17-
private static _127 test;
18-
private static List<String> wordList;
14+
private static _127.Solution1 solution1;
15+
private static List<String> wordList;
1916

20-
@BeforeClass
21-
public static void setup() {
22-
test = new _127();
23-
}
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _127.Solution1();
20+
}
2421

25-
@Test
26-
public void test1() {
27-
wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log"));
28-
assertEquals(0, test.ladderLength("hit", "cog", wordList));
29-
}
22+
@Test
23+
public void test1() {
24+
wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log"));
25+
assertEquals(0, solution1.ladderLength("hit", "cog", wordList));
26+
}
3027

31-
@Test
32-
public void test2() {
33-
wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog"));
34-
assertEquals(5, test.ladderLength("hit", "cog", wordList));
35-
}
28+
@Test
29+
public void test2() {
30+
wordList = new ArrayList<>(Arrays.asList("hot", "dot", "dog", "lot", "log", "cog"));
31+
assertEquals(5, solution1.ladderLength("hit", "cog", wordList));
32+
}
3633
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy