Skip to content

Commit fd703c7

Browse files
refactor 524
1 parent 26e14a9 commit fd703c7

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,22 @@
3232
*/
3333
public class _524 {
3434

35-
public String findLongestWord(String s, List<String> d) {
36-
Collections.sort(d, (a, b) -> a.length() == b.length() ? a.compareTo(b) : b.length() - a.length());
37-
for (String dictWord : d) {
38-
int i = 0;
39-
for (char c : s.toCharArray()) {
40-
if (i < dictWord.length() && dictWord.charAt(i) == c) {
41-
i++;
35+
public static class Solution1 {
36+
public String findLongestWord(String s, List<String> d) {
37+
Collections.sort(d, (a, b) -> a.length() == b.length() ? a.compareTo(b) : b.length() - a.length());
38+
for (String dictWord : d) {
39+
int i = 0;
40+
for (char c : s.toCharArray()) {
41+
if (i < dictWord.length() && dictWord.charAt(i) == c) {
42+
i++;
43+
}
44+
}
45+
if (i == dictWord.length()) {
46+
return dictWord;
4247
}
4348
}
44-
if (i == dictWord.length()) {
45-
return dictWord;
46-
}
49+
return "";
4750
}
48-
return "";
4951
}
5052

5153
}

src/test/java/com/fishercoder/_524Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
* Created by fishercoder on 4/30/17.
1414
*/
1515
public class _524Test {
16-
private static _524 test;
16+
private static _524.Solution1 solution1;
1717
private static String expected;
1818
private static String actual;
1919
private static String s;
2020
private static ArrayList d;
2121

2222
@BeforeClass
2323
public static void setup() {
24-
test = new _524();
24+
solution1 = new _524.Solution1();
2525
}
2626

2727
@Test
2828
public void test1() {
2929
d = new ArrayList(Arrays.asList("ale", "apple", "monkey", "plea"));
3030
s = "abpcplea";
3131
expected = "apple";
32-
actual = test.findLongestWord(expected, d);
32+
actual = solution1.findLongestWord(expected, d);
3333
assertEquals(expected, actual);
3434
}
3535

@@ -38,7 +38,7 @@ public void test2() {
3838
d = new ArrayList(Arrays.asList("a", "b", "c"));
3939
s = "abpcplea";
4040
expected = "a";
41-
actual = test.findLongestWord(expected, d);
41+
actual = solution1.findLongestWord(expected, d);
4242
assertEquals(expected, actual);
4343
}
4444

@@ -47,7 +47,7 @@ public void test3() {
4747
d = new ArrayList(Arrays.asList("apple", "ewaf", "awefawfwaf", "awef", "awefe", "ewafeffewafewf"));
4848
s = "aewfafwafjlwajflwajflwafj";
4949
expected = "ewaf";
50-
actual = test.findLongestWord(expected, d);
50+
actual = solution1.findLongestWord(expected, d);
5151
assertEquals(expected, actual);
5252
}
5353
}

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