-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (34 loc) · 807 Bytes
/
Copy pathSolution.java
File metadata and controls
38 lines (34 loc) · 807 Bytes
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
import java.util.Collections;
import java.util.List;
public class Solution
{
public String findLongestWord(String s, List<String> d)
{
Collections.sort(d, (a, b) -> a.length() != b.length() ? b.length() - a.length() : a.compareTo(b));
for (String wrd : d)
{
if (isSubstring(s, wrd))
{
return wrd;
}
}
return "";
}
private boolean isSubstring(String s, String d)
{
if (s.length() < d.length())
{
return false;
}
int i = 0, j = 0;
while (i < s.length() && j < d.length())
{
if (s.charAt(i) == d.charAt(j))
{
j++;
}
i++;
}
return j == d.length();
}
}