-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteSystem.java
More file actions
100 lines (87 loc) · 2.44 KB
/
AutocompleteSystem.java
File metadata and controls
100 lines (87 loc) · 2.44 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
100
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AutocompleteSystem
{
TrieAS root = new TrieAS();
public AutocompleteSystem( String[] sentences, int[] times )
{
for ( int i = 0; i < sentences.length; i++ )
{
root.update( sentences[i], times[i] );
}
}
StringBuilder find = new StringBuilder();
public List<String> input( char c )
{
if ( c == '#' )
{
root.update( find.toString(), 1 );
find.delete( 0, find.length() );
return new ArrayList<>();
}
find.append( c );
List<String[]> list = new ArrayList<>();
findStr( find.toString(), list );
Collections.sort( list, new Comparator<String[]>()
{
@Override
public int compare( String[] o1, String[] o2 )
{
int v1 = Integer.valueOf( o1[1] ), v2 = Integer.valueOf( o2[1] );
return v1 != v2 ? v2 - v1 : o1[0].compareTo( o2[0] );
}
} );
List<String> ret = new ArrayList<>();
for ( int i = 0, n = list.size(); i < Math.min( 3, n ); i++ )
{
ret.add( list.get( i )[0] );
}
return ret;
}
void findStr( String string, List<String[]> list )
{
TrieAS r = root;
for ( char k : string.toCharArray() )
{
int pos = k == ' ' ? 26 : k - 'a';
if ( r._child[pos] == null )
return;
r = r._child[pos];
}
for ( String s : r.map.keySet() )
list.add( new String[] { s, String.valueOf( r.map.get( s ) ) } );
}
public static void main( String[] args )
{
String[] s = { "i love you", "island", "iroman", "i love leetcode" };
int[] t = { 5, 3, 2, 2 };
AutocompleteSystem ats = new AutocompleteSystem( s, t );
String[][] inp = { { "i" }, { " " }, { "a" }, { "#" }, { "i" }, { " " }, { "a" }, { "#" }, { "i" }, { " " }, { "a" }, { "#" } };
for ( int i = 0; i < inp.length; i++ )
System.out.println( ats.input( inp[i][0].toCharArray()[0] ) );
}
}
class TrieAS
{
TrieAS[] _child = new TrieAS[27];
Map<String, Integer> map = new HashMap<>();
List<String> _str = new ArrayList<>();
List<Integer> _hotness = new ArrayList<>();
void update( String string, int hotness )
{
TrieAS root = this;
for ( char k : string.toCharArray() )
{
int pos = k == ' ' ? 26 : k - 'a';
if ( root._child[pos] == null )
root._child[pos] = new TrieAS();
root.map.put( string, root.map.getOrDefault( string, 0 ) + hotness );
root = root._child[pos];
}
root.map.put( string, root.map.getOrDefault( string, 0 ) + hotness );
}
}