Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b71081b

Browse files
20200907
1 parent 8a6d67e commit b71081b

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

‎Java/290.word-pattern.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=290 lang=java
3+
*
4+
* [290] Word Pattern
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
/*
10+
time: O(n^2) containsValue() costs O(n)
11+
space: O(n)
12+
*/
13+
public boolean wordPattern(String pattern, String str) {
14+
String[] words = str.split(" ");
15+
if (pattern.length() != words.length) return false;
16+
Map<String, Character> map = new HashMap<>();
17+
for (int i = 0; i < pattern.length(); ++i) {
18+
if (map.containsKey(words[i])) {
19+
if (pattern.charAt(i) != map.get(words[i])) {
20+
return false;
21+
}
22+
} else if (map.containsValue(pattern.charAt(i))) {
23+
return false;
24+
} else {
25+
map.put(words[i], pattern.charAt(i));
26+
}
27+
}
28+
return true;
29+
}
30+
}
31+
// @lc code=end
32+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* @lc app=leetcode id=642 lang=java
3+
*
4+
* [642] Design Search Autocomplete System
5+
*/
6+
7+
// @lc code=start
8+
9+
/*
10+
Trie记录前缀搜索的字符
11+
对于每一个字符,利用一个hashmap来记录与之有关的字符串和对应的count
12+
input的时候利用一个PriorityQueue来返回count最大的三个结果
13+
time: O(nl) for building the trie, n = number of sentences and l = avg length of sentence
14+
O(l + mlogm) m = size of map
15+
space: O(nl) and memory of each map
16+
*/
17+
class AutocompleteSystem {
18+
class TrieNode {
19+
TrieNode[] children;
20+
Map<String, Integer> count;
21+
public TrieNode() {
22+
children = new TrieNode[128];
23+
count = new HashMap<>();
24+
}
25+
}
26+
27+
class Pair {
28+
String key;
29+
int count;
30+
public Pair(String key, int count) {
31+
this.key = key;
32+
this.count = count;
33+
}
34+
}
35+
TrieNode root;
36+
StringBuilder sb;
37+
public AutocompleteSystem(String[] sentences, int[] times) {
38+
root = new TrieNode();
39+
sb = new StringBuilder();
40+
for (int i = 0; i < sentences.length; ++i) {
41+
add(sentences[i], times[i]);
42+
}
43+
}
44+
45+
public List<String> input(char c) {
46+
List<String> res = new ArrayList<>();
47+
if (c == '#') {
48+
add(sb.toString(), 1);
49+
sb.setLength(0);
50+
return res;
51+
}
52+
sb.append(c);
53+
TrieNode cur = root;
54+
for (int i = 0; i < sb.length(); ++i) {
55+
if (cur.children[sb.charAt(i)] == null) return res;
56+
cur = cur.children[sb.charAt(i)];
57+
}
58+
PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) ->
59+
a.count == b.count ? a.key.compareTo(b.key) : b.count - a.count);
60+
for (String key : cur.count.keySet()) {
61+
pq.add(new Pair(key, cur.count.get(key)));
62+
}
63+
for (int i = 0; i < 3 && !pq.isEmpty(); ++i) {
64+
res.add(pq.poll().key);
65+
}
66+
return res;
67+
}
68+
69+
private void add(String sentence, int times) {
70+
TrieNode cur = root;
71+
for (char c : sentence.toCharArray()) {
72+
if (cur.children[c] == null) {
73+
cur.children[c] = new TrieNode();
74+
}
75+
cur = cur.children[c];
76+
cur.count.put(sentence, cur.count.getOrDefault(sentence, 0) + times);
77+
}
78+
}
79+
}
80+
81+
/**
82+
* Your AutocompleteSystem object will be instantiated and called as such:
83+
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
84+
* List<String> param_1 = obj.input(c);
85+
*/
86+
// @lc code=end
87+

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
:- | :-:
114114
[146. LRU Cache](https://leetcode.com/problems/lru-cache/) | [HashMap + Doubly Linked List](https://github.com/Yukinichi/leetcode/blob/master/Java/146.lru-cache.java) \| [Java LinkedHashMap](https://github.com/Yukinichi/leetcode/blob/master/Java/146.lru-cache-solution2.java)
115115
[460. LFU Cache](https://leetcode.com/problems/lfu-cache/) | [HashMap + TreeSet(Ologn)](https://github.com/Yukinichi/leetcode/blob/master/Java/460.lfu-cache.java) \| [HashMap + DList(O(1))](https://github.com/Yukinichi/leetcode/blob/master/Java/460.lfu-cache-2.java)
116+
[642. Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/) | [Trie + PriorityQueue](https://github.com/Yukinichi/leetcode/blob/master/Java/642.design-search-autocomplete-system.java)
116117
[895. Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/) | [思路同LFU Cache](https://github.com/Yukinichi/leetcode/blob/master/Java/895.maximum-frequency-stack.java)
117118
[705. Design HashSet](https://leetcode.com/problems/design-hashset/) | [linkedlist handle hashing collision](https://github.com/Yukinichi/leetcode/blob/master/Java/705.design-hash-set.java)
118119
[706. Design HashMap](https://leetcode.com/problems/design-hashmap/) | [linkedlist handle hashing collision](https://github.com/Yukinichi/leetcode/blob/master/Java/706.design-hash-map.java)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /