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