Skip to content

Commit ddefa25

Browse files
committed
feat: add solutions to lcci problem: No.16.02
No.16.02.Words Frequency
1 parent 0532cec commit ddefa25

File tree

11 files changed

+204
-57
lines changed

11 files changed

+204
-57
lines changed

lcci/16.02.Words Frequency/README.md

+72-17
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ wordsFrequency.get("pen"); //返回1
3131

3232
<!-- 这里可写通用的实现逻辑 -->
3333

34-
计数器实现。
34+
**方法一:哈希表**
35+
36+
我们用哈希表 `cnt` 统计每个单词出现的次数,`get` 函数直接返回 `cnt[word]` 即可。
37+
38+
初始化哈希表 `cnt` 的时间复杂度为 $O(n)$,其中 $n$ 为 `book` 的长度。`get` 函数的时间复杂度为 $O(1)$。空间复杂度为 $O(n)$。
3539

3640
<!-- tabs:start -->
3741

@@ -42,10 +46,10 @@ wordsFrequency.get("pen"); //返回1
4246
```python
4347
class WordsFrequency:
4448
def __init__(self, book: List[str]):
45-
self.counter = Counter(book)
49+
self.cnt = Counter(book)
4650

4751
def get(self, word: str) -> int:
48-
return self.counter[word]
52+
return self.cnt[word]
4953

5054

5155
# Your WordsFrequency object will be instantiated and called as such:
@@ -59,17 +63,16 @@ class WordsFrequency:
5963

6064
```java
6165
class WordsFrequency {
62-
63-
private Map<String, Integer> counter = new HashMap<>();
66+
private Map<String, Integer> cnt = new HashMap<>();
6467

6568
public WordsFrequency(String[] book) {
66-
for (String word : book) {
67-
counter.put(word, counter.getOrDefault(word, 0) + 1);
69+
for (String x : book) {
70+
cnt.merge(x, 1, Integer::sum);
6871
}
6972
}
7073

7174
public int get(String word) {
72-
return counter.containsKey(word) ? counter.get(word) : 0;
75+
return cnt.getOrDefault(word, 0);
7376
}
7477
}
7578

@@ -80,16 +83,68 @@ class WordsFrequency {
8083
*/
8184
```
8285

86+
### **C++**
87+
88+
```cpp
89+
class WordsFrequency {
90+
public:
91+
WordsFrequency(vector<string>& book) {
92+
for (auto& x : book) {
93+
++cnt[x];
94+
}
95+
}
96+
97+
int get(string word) {
98+
return cnt[word];
99+
}
100+
101+
private:
102+
unordered_map<string, int> cnt;
103+
};
104+
105+
/**
106+
* Your WordsFrequency object will be instantiated and called as such:
107+
* WordsFrequency* obj = new WordsFrequency(book);
108+
* int param_1 = obj->get(word);
109+
*/
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
type WordsFrequency struct {
116+
cnt map[string]int
117+
}
118+
119+
func Constructor(book []string) WordsFrequency {
120+
cnt := map[string]int{}
121+
for _, x := range book {
122+
cnt[x]++
123+
}
124+
return WordsFrequency{cnt}
125+
}
126+
127+
func (this *WordsFrequency) Get(word string) int {
128+
return this.cnt[word]
129+
}
130+
131+
/**
132+
* Your WordsFrequency object will be instantiated and called as such:
133+
* obj := Constructor(book);
134+
* param_1 := obj.Get(word);
135+
*/
136+
```
137+
83138
### **JavaScript**
84139

85140
```js
86141
/**
87142
* @param {string[]} book
88143
*/
89144
var WordsFrequency = function (book) {
90-
this.counter = {};
91-
for (const word of book) {
92-
this.counter[word] = (this.counter[word] || 0) + 1;
145+
this.cnt = new Map();
146+
for (const x of book) {
147+
this.cnt.set(x, (this.cnt.get(x) || 0) + 1);
93148
}
94149
};
95150

@@ -98,7 +153,7 @@ var WordsFrequency = function (book) {
98153
* @return {number}
99154
*/
100155
WordsFrequency.prototype.get = function (word) {
101-
return this.counter[word] || 0;
156+
return this.cnt.get(word) || 0;
102157
};
103158

104159
/**
@@ -112,18 +167,18 @@ WordsFrequency.prototype.get = function (word) {
112167

113168
```ts
114169
class WordsFrequency {
115-
private map: Map<string, number>;
170+
private cnt: Map<string, number>;
116171

117172
constructor(book: string[]) {
118-
const map = new Map<string, number>();
173+
const cnt = new Map<string, number>();
119174
for (const word of book) {
120-
map.set(word, (map.get(word) ?? 0) + 1);
175+
cnt.set(word, (cnt.get(word) ?? 0) + 1);
121176
}
122-
this.map = map;
177+
this.cnt = cnt;
123178
}
124179

125180
get(word: string): number {
126-
return this.map.get(word) ?? 0;
181+
return this.cnt.get(word) ?? 0;
127182
}
128183
}
129184

lcci/16.02.Words Frequency/README_EN.md

+68-17
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ wordsFrequency.get(&quot;pen&quot;); //returns 1
4949
```python
5050
class WordsFrequency:
5151
def __init__(self, book: List[str]):
52-
self.counter = Counter(book)
52+
self.cnt = Counter(book)
5353

5454
def get(self, word: str) -> int:
55-
return self.counter[word]
55+
return self.cnt[word]
5656

5757

5858
# Your WordsFrequency object will be instantiated and called as such:
@@ -64,17 +64,16 @@ class WordsFrequency:
6464

6565
```java
6666
class WordsFrequency {
67-
68-
private Map<String, Integer> counter = new HashMap<>();
67+
private Map<String, Integer> cnt = new HashMap<>();
6968

7069
public WordsFrequency(String[] book) {
71-
for (String word : book) {
72-
counter.put(word, counter.getOrDefault(word, 0) + 1);
70+
for (String x : book) {
71+
cnt.merge(x, 1, Integer::sum);
7372
}
7473
}
75-
74+
7675
public int get(String word) {
77-
return counter.containsKey(word) ? counter.get(word) : 0;
76+
return cnt.getOrDefault(word, 0);
7877
}
7978
}
8079

@@ -85,16 +84,68 @@ class WordsFrequency {
8584
*/
8685
```
8786

87+
### **C++**
88+
89+
```cpp
90+
class WordsFrequency {
91+
public:
92+
WordsFrequency(vector<string>& book) {
93+
for (auto& x : book) {
94+
++cnt[x];
95+
}
96+
}
97+
98+
int get(string word) {
99+
return cnt[word];
100+
}
101+
102+
private:
103+
unordered_map<string, int> cnt;
104+
};
105+
106+
/**
107+
* Your WordsFrequency object will be instantiated and called as such:
108+
* WordsFrequency* obj = new WordsFrequency(book);
109+
* int param_1 = obj->get(word);
110+
*/
111+
```
112+
113+
### **Go**
114+
115+
```go
116+
type WordsFrequency struct {
117+
cnt map[string]int
118+
}
119+
120+
func Constructor(book []string) WordsFrequency {
121+
cnt := map[string]int{}
122+
for _, x := range book {
123+
cnt[x]++
124+
}
125+
return WordsFrequency{cnt}
126+
}
127+
128+
func (this *WordsFrequency) Get(word string) int {
129+
return this.cnt[word]
130+
}
131+
132+
/**
133+
* Your WordsFrequency object will be instantiated and called as such:
134+
* obj := Constructor(book);
135+
* param_1 := obj.Get(word);
136+
*/
137+
```
138+
88139
### **JavaScript**
89140

90141
```js
91142
/**
92143
* @param {string[]} book
93144
*/
94145
var WordsFrequency = function (book) {
95-
this.counter = {};
96-
for (const word of book) {
97-
this.counter[word] = (this.counter[word] || 0) + 1;
146+
this.cnt = new Map();
147+
for (const x of book) {
148+
this.cnt.set(x, (this.cnt.get(x) || 0) + 1);
98149
}
99150
};
100151

@@ -103,7 +154,7 @@ var WordsFrequency = function (book) {
103154
* @return {number}
104155
*/
105156
WordsFrequency.prototype.get = function (word) {
106-
return this.counter[word] || 0;
157+
return this.cnt.get(word) || 0;
107158
};
108159

109160
/**
@@ -117,18 +168,18 @@ WordsFrequency.prototype.get = function (word) {
117168

118169
```ts
119170
class WordsFrequency {
120-
private map: Map<string, number>;
171+
private cnt: Map<string, number>;
121172

122173
constructor(book: string[]) {
123-
const map = new Map<string, number>();
174+
const cnt = new Map<string, number>();
124175
for (const word of book) {
125-
map.set(word, (map.get(word) ?? 0) + 1);
176+
cnt.set(word, (cnt.get(word) ?? 0) + 1);
126177
}
127-
this.map = map;
178+
this.cnt = cnt;
128179
}
129180

130181
get(word: string): number {
131-
return this.map.get(word) ?? 0;
182+
return this.cnt.get(word) ?? 0;
132183
}
133184
}
134185

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class WordsFrequency {
2+
public:
3+
WordsFrequency(vector<string>& book) {
4+
for (auto& x : book) {
5+
++cnt[x];
6+
}
7+
}
8+
9+
int get(string word) {
10+
return cnt[word];
11+
}
12+
13+
private:
14+
unordered_map<string, int> cnt;
15+
};
16+
17+
/**
18+
* Your WordsFrequency object will be instantiated and called as such:
19+
* WordsFrequency* obj = new WordsFrequency(book);
20+
* int param_1 = obj->get(word);
21+
*/
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type WordsFrequency struct {
2+
cnt map[string]int
3+
}
4+
5+
func Constructor(book []string) WordsFrequency {
6+
cnt := map[string]int{}
7+
for _, x := range book {
8+
cnt[x]++
9+
}
10+
return WordsFrequency{cnt}
11+
}
12+
13+
func (this *WordsFrequency) Get(word string) int {
14+
return this.cnt[word]
15+
}
16+
17+
/**
18+
* Your WordsFrequency object will be instantiated and called as such:
19+
* obj := Constructor(book);
20+
* param_1 := obj.Get(word);
21+
*/

lcci/16.02.Words Frequency/Solution.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class WordsFrequency {
2-
3-
private Map<String, Integer> counter = new HashMap<>();
2+
private Map<String, Integer> cnt = new HashMap<>();
43

54
public WordsFrequency(String[] book) {
6-
for (String word : book) {
7-
counter.put(word, counter.getOrDefault(word, 0) + 1);
5+
for (String x : book) {
6+
cnt.merge(x, 1, Integer::sum);
87
}
98
}
10-
9+
1110
public int get(String word) {
12-
return counter.containsKey(word) ? counter.get(word) : 0;
11+
return cnt.getOrDefault(word, 0);
1312
}
1413
}
1514

lcci/16.02.Words Frequency/Solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* @param {string[]} book
33
*/
44
var WordsFrequency = function (book) {
5-
this.counter = {};
6-
for (const word of book) {
7-
this.counter[word] = (this.counter[word] || 0) + 1;
5+
this.cnt = new Map();
6+
for (const x of book) {
7+
this.cnt.set(x, (this.cnt.get(x) || 0) + 1);
88
}
99
};
1010

@@ -13,7 +13,7 @@ var WordsFrequency = function (book) {
1313
* @return {number}
1414
*/
1515
WordsFrequency.prototype.get = function (word) {
16-
return this.counter[word] || 0;
16+
return this.cnt.get(word) || 0;
1717
};
1818

1919
/**

lcci/16.02.Words Frequency/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class WordsFrequency:
22
def __init__(self, book: List[str]):
3-
self.counter = Counter(book)
3+
self.cnt = Counter(book)
44

55
def get(self, word: str) -> int:
6-
return self.counter[word]
6+
return self.cnt[word]
77

88

99
# Your WordsFrequency object will be instantiated and called as such:

0 commit comments

Comments
 (0)