-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution524.java
More file actions
33 lines (31 loc) · 823 Bytes
/
Copy pathsolution524.java
File metadata and controls
33 lines (31 loc) · 823 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
import java.util.List;
public class solution524 {
public String findLongestWord(String s, List<String> d) {
String longestWord = "";
for(String target : d)
{
int l1 = longestWord.length(),l2 = target.length();
if(l1>l2 ||(l1 == l2&&longestWord.compareTo(target)<0))
{
continue;
}
if(isValid(s,target))
{
longestWord = target;
}
}
return longestWord;
}
private boolean isValid(String s, String target) {
int i =0,j = 0;
while(i < s.length()&&j<target.length())
{
if(s.charAt(i) == target.charAt(j))
{
j++;
}
i++;
}
return j==target.length();
}
}