-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (30 loc) · 873 Bytes
/
Solution.java
File metadata and controls
34 lines (30 loc) · 873 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
package leetCode_524;
import java.util.List;
/**
* @author dimdark
* @date 2017-09-20
* @time 8:01 PM
*/
public class Solution {
public String findLongestWord(String s, List<String> d) { // O(n^2)
String maxSubString = "";
int maxSubLen = 0;
char[] cs, cw;
cs = s.toCharArray();
for (String w : d) { // w : word
cw = w.toCharArray();
int i = 0, j = 0;
while (i < cs.length && j < cw.length) {
if (cs[i] == cw[j]) ++j;
++i;
}
if (j >= cw.length) { // sub sequence
if (maxSubLen < cw.length || (maxSubLen == cw.length && maxSubString.compareTo(w) > 0)) {
maxSubLen = cw.length;
maxSubString = w;
}
}
}
return maxSubString;
}
}