-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_524.java
27 lines (24 loc) · 797 Bytes
/
_524.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
package com.fishercoder.solutions.firstthousand;
import java.util.Collections;
import java.util.List;
public class _524 {
public static class Solution1 {
public String findLongestWord(String s, List<String> d) {
Collections.sort(
d,
(a, b) -> a.length() == b.length() ? a.compareTo(b) : b.length() - a.length());
for (String dictWord : d) {
int i = 0;
for (char c : s.toCharArray()) {
if (i < dictWord.length() && dictWord.charAt(i) == c) {
i++;
}
}
if (i == dictWord.length()) {
return dictWord;
}
}
return "";
}
}
}