Skip to content

Commit 54932a3

Browse files
committed
feat: add solutions to lcci problem: No.16.02. Word Frequency
1 parent cd1ff77 commit 54932a3

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

lcci/16.02.Words Frequency/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,81 @@ wordsFrequency.get("pen"); //返回1
3131

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

34+
计数器实现。
35+
3436
<!-- tabs:start -->
3537

3638
### **Python3**
3739

3840
<!-- 这里可写当前语言的特殊实现逻辑 -->
3941

4042
```python
43+
class WordsFrequency:
44+
45+
def __init__(self, book: List[str]):
46+
self.counter = collections.Counter(book)
4147

48+
def get(self, word: str) -> int:
49+
return self.counter[word]
50+
51+
# Your WordsFrequency object will be instantiated and called as such:
52+
# obj = WordsFrequency(book)
53+
# param_1 = obj.get(word)
4254
```
4355

4456
### **Java**
4557

4658
<!-- 这里可写当前语言的特殊实现逻辑 -->
4759

4860
```java
61+
class WordsFrequency {
62+
63+
private Map<String, Integer> counter = new HashMap<>();
64+
65+
public WordsFrequency(String[] book) {
66+
for (String word : book) {
67+
counter.put(word, counter.getOrDefault(word, 0) + 1);
68+
}
69+
}
70+
71+
public int get(String word) {
72+
return counter.containsKey(word) ? counter.get(word) : 0;
73+
}
74+
}
75+
76+
/**
77+
* Your WordsFrequency object will be instantiated and called as such:
78+
* WordsFrequency obj = new WordsFrequency(book);
79+
* int param_1 = obj.get(word);
80+
*/
81+
```
4982

83+
### **JavaScript**
84+
85+
```js
86+
/**
87+
* @param {string[]} book
88+
*/
89+
var WordsFrequency = function (book) {
90+
this.counter = {};
91+
for (const word of book) {
92+
this.counter[word] = (this.counter[word] || 0) + 1;
93+
}
94+
};
95+
96+
/**
97+
* @param {string} word
98+
* @return {number}
99+
*/
100+
WordsFrequency.prototype.get = function (word) {
101+
return this.counter[word] || 0;
102+
};
103+
104+
/**
105+
* Your WordsFrequency object will be instantiated and called as such:
106+
* var obj = new WordsFrequency(book)
107+
* var param_1 = obj.get(word)
108+
*/
50109
```
51110

52111
### **...**

lcci/16.02.Words Frequency/README_EN.md

+57
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,70 @@ wordsFrequency.get(&quot;pen&quot;); //returns 1
4747
### **Python3**
4848

4949
```python
50+
class WordsFrequency:
5051

52+
def __init__(self, book: List[str]):
53+
self.counter = collections.Counter(book)
54+
55+
def get(self, word: str) -> int:
56+
return self.counter[word]
57+
58+
# Your WordsFrequency object will be instantiated and called as such:
59+
# obj = WordsFrequency(book)
60+
# param_1 = obj.get(word)
5161
```
5262

5363
### **Java**
5464

5565
```java
66+
class WordsFrequency {
67+
68+
private Map<String, Integer> counter = new HashMap<>();
69+
70+
public WordsFrequency(String[] book) {
71+
for (String word : book) {
72+
counter.put(word, counter.getOrDefault(word, 0) + 1);
73+
}
74+
}
75+
76+
public int get(String word) {
77+
return counter.containsKey(word) ? counter.get(word) : 0;
78+
}
79+
}
80+
81+
/**
82+
* Your WordsFrequency object will be instantiated and called as such:
83+
* WordsFrequency obj = new WordsFrequency(book);
84+
* int param_1 = obj.get(word);
85+
*/
86+
```
5687

88+
### **JavaScript**
89+
90+
```js
91+
/**
92+
* @param {string[]} book
93+
*/
94+
var WordsFrequency = function (book) {
95+
this.counter = {};
96+
for (const word of book) {
97+
this.counter[word] = (this.counter[word] || 0) + 1;
98+
}
99+
};
100+
101+
/**
102+
* @param {string} word
103+
* @return {number}
104+
*/
105+
WordsFrequency.prototype.get = function (word) {
106+
return this.counter[word] || 0;
107+
};
108+
109+
/**
110+
* Your WordsFrequency object will be instantiated and called as such:
111+
* var obj = new WordsFrequency(book)
112+
* var param_1 = obj.get(word)
113+
*/
57114
```
58115

59116
### **...**
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class WordsFrequency {
2+
3+
private Map<String, Integer> counter = new HashMap<>();
4+
5+
public WordsFrequency(String[] book) {
6+
for (String word : book) {
7+
counter.put(word, counter.getOrDefault(word, 0) + 1);
8+
}
9+
}
10+
11+
public int get(String word) {
12+
return counter.containsKey(word) ? counter.get(word) : 0;
13+
}
14+
}
15+
16+
/**
17+
* Your WordsFrequency object will be instantiated and called as such:
18+
* WordsFrequency obj = new WordsFrequency(book);
19+
* int param_1 = obj.get(word);
20+
*/
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string[]} book
3+
*/
4+
var WordsFrequency = function (book) {
5+
this.counter = {};
6+
for (const word of book) {
7+
this.counter[word] = (this.counter[word] || 0) + 1;
8+
}
9+
};
10+
11+
/**
12+
* @param {string} word
13+
* @return {number}
14+
*/
15+
WordsFrequency.prototype.get = function (word) {
16+
return this.counter[word] || 0;
17+
};
18+
19+
/**
20+
* Your WordsFrequency object will be instantiated and called as such:
21+
* var obj = new WordsFrequency(book)
22+
* var param_1 = obj.get(word)
23+
*/
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class WordsFrequency:
2+
3+
def __init__(self, book: List[str]):
4+
self.counter = collections.Counter(book)
5+
6+
def get(self, word: str) -> int:
7+
return self.counter[word]
8+
9+
# Your WordsFrequency object will be instantiated and called as such:
10+
# obj = WordsFrequency(book)
11+
# param_1 = obj.get(word)

0 commit comments

Comments
 (0)