-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path524.java
More file actions
99 lines (87 loc) · 2.79 KB
/
524.java
File metadata and controls
99 lines (87 loc) · 2.79 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
__________________________________________________________________________________________________
sample 3 ms submission
class Solution {
private int[][] arrays;
public String findLongestWord(String s, List<String> d) {
if(s.length()==0)
return "";
createArrays(s);
int longestMatch=0;
String current="";
String next;
for(int i=0;i<d.size();++i)
{
next = d.get(i);
if(next.length()>longestMatch || (next.length()==longestMatch && next.compareTo(current)<0))
{
if(canMakeStringFast(next))
{
current = next;
longestMatch = current.length();
}
}
}
return current;
}
private boolean canMakeString(String s, String l)
{
int left=0, right=0;
while(true){
if(s.charAt(left) == l.charAt(right)){
++right;
}
++left;
if(right==l.length())
return true;
if(left==s.length())
return false;
}
}
private boolean canMakeStringFast(String l)
{
int index = 0;
for(int i=0;i<l.length();++i)
{
index = arrays[index][l.charAt(i)-'a'];
if(index==0)
return false;
}
return true;
}
private void createArrays(String s)
{
arrays = new int[s.length()+1][26];
int posp1 = s.length();
int pos = posp1-1;
while(true){
arrays[pos][s.charAt(pos)-'a'] = posp1;
--pos;
--posp1;
if(pos<0)
break;
for( int i =0; i< 26;++i)
arrays[pos][i]=arrays[posp1][i];
}
}
}
__________________________________________________________________________________________________
sample 36156 kb submission
class Solution {
public String findLongestWord(String s, List<String> d){
/*
if a.length() == b.length(), a and b are comparable, we sort a and b in lexicographical order
Else, we compare a and b according to their length.
*/
Collections.sort(d, (a, b) -> a.length() == b.length()?
a.compareTo(b) : b.length() - a.length());
for(String word: d){
int count = 0;
for(int i = 0; i < s.length(); i++){
if(count < word.length() && s.charAt(i) == word.charAt(count)) count++;
}
if(count == word.length()) return word;
}
return "";
}
}
__________________________________________________________________________________________________