-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution524.java
38 lines (33 loc) · 1.03 KB
/
Solution524.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package leetcode.doublepointer;
import java.util.Arrays;
import java.util.List;
public class Solution524 {
public static void main(String[] args) {
System.out.println(new Solution524().findLongestWord("abpcplea", Arrays.asList("ale", "apple", "monkey", "plea")));
}
public String findLongestWord(String s, List<String> dictionary) {
dictionary.sort((x, y) -> {
if (x.length() != y.length()) {
return y.length() - x.length();
}
return x.compareTo(y);
});
String res = "";
for (String d : dictionary) {
int dIndex = 0, sIndex = 0;
while (dIndex < d.length() && sIndex < s.length()) {
if (d.charAt(dIndex) != s.charAt(sIndex)) {
sIndex++;
continue;
}
dIndex++;
sIndex++;
}
if (dIndex == d.length()) {
res = d;
break;
}
}
return res;
}
}