Skip to content

Commit 547a7b4

Browse files
committed
feat: add solutions to lc problem: No.0884
No.0884.Uncommon Words from Two Sentences
1 parent 3bd061a commit 547a7b4

File tree

8 files changed

+197
-155
lines changed

8 files changed

+197
-155
lines changed

solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md

+76-58
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
使用哈希表 `HashMap<String, Integer>` 记录字符串出现的次数,完成所有记录后遍历哈希表,找到出现次数为 1 的 `key`,存入数组当中并返回。
49+
**方法一:哈希表**
5050

51-
由于只关注出现 1 次的字符串,可以将 `value` 类型定为 `Boolean`。首次记录为 `true`,后续再出现则改为 `false`
51+
根据题目描述,只要单词出现一次,就符合题目要求。因此,我们用哈希表 $cnt$ 记录所有单词以及出现的次数。
52+
53+
然后遍历哈希表,取出所有出现次数为 $1$ 的字符串即可。
54+
55+
时间复杂度 $O(m + n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别是字符串 $s1$ 和 $s2$ 的长度。
5256

5357
<!-- tabs:start -->
5458

@@ -59,8 +63,8 @@
5963
```python
6064
class Solution:
6165
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
62-
c = Counter(s1.split()) + Counter(s2.split())
63-
return [w for w, n in c.items() if n == 1]
66+
cnt = Counter(s1.split()) + Counter(s2.split())
67+
return [s for s, v in cnt.items() if v == 1]
6468
```
6569

6670
### **Java**
@@ -69,43 +73,22 @@ class Solution:
6973

7074
```java
7175
class Solution {
72-
7376
public String[] uncommonFromSentences(String s1, String s2) {
74-
Map<String, Integer> counter = new HashMap<>();
75-
add(s1, counter);
76-
add(s2, counter);
77+
Map<String, Integer> cnt = new HashMap<>();
78+
for (String s : s1.split(" ")) {
79+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
80+
}
81+
for (String s : s2.split(" ")) {
82+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
83+
}
7784
List<String> ans = new ArrayList<>();
78-
for (Map.Entry<String, Integer> e : counter.entrySet()) {
85+
for (var e : cnt.entrySet()) {
7986
if (e.getValue() == 1) {
8087
ans.add(e.getKey());
8188
}
8289
}
8390
return ans.toArray(new String[0]);
8491
}
85-
86-
private void add(String s, Map<String, Integer> counter) {
87-
for (String w : s.split(" ")) {
88-
counter.put(w, counter.getOrDefault(w, 0) + 1);
89-
}
90-
}
91-
}
92-
```
93-
94-
### **TypeScript**
95-
96-
```ts
97-
function uncommonFromSentences(s1: string, s2: string): string[] {
98-
let hashMap: Map<string, number> = new Map();
99-
for (let str of [...s1.split(' '), ...s2.split(' ')]) {
100-
hashMap.set(str, (hashMap.get(str) || 0) + 1);
101-
}
102-
let ans: Array<string> = [];
103-
for (let [key, count] of hashMap.entries()) {
104-
if (count == 1) {
105-
ans.push(key);
106-
}
107-
}
108-
return ans;
10992
}
11093
```
11194

@@ -115,20 +98,16 @@ function uncommonFromSentences(s1: string, s2: string): string[] {
11598
class Solution {
11699
public:
117100
vector<string> uncommonFromSentences(string s1, string s2) {
118-
unordered_map<string, int> counter;
119-
120-
auto add = [&](const string& s) {
101+
unordered_map<string, int> cnt;
102+
auto add = [&](string& s) {
121103
stringstream ss(s);
122-
string word;
123-
while (ss >> word) ++counter[move(word)];
104+
string w;
105+
while (ss >> w) ++cnt[move(w)];
124106
};
125-
126107
add(s1);
127108
add(s2);
128109
vector<string> ans;
129-
for (auto& [word, n] : counter)
130-
if (n == 1)
131-
ans.push_back(word);
110+
for (auto& [s, v] : cnt) if (v == 1) ans.emplace_back(s);
132111
return ans;
133112
}
134113
};
@@ -137,22 +116,38 @@ public:
137116
### **Go**
138117
139118
```go
140-
func uncommonFromSentences(s1 string, s2 string) []string {
141-
counter := make(map[string]int)
142-
add := func(s string) {
143-
for _, w := range strings.Split(s, " ") {
144-
counter[w]++
145-
}
146-
}
147-
add(s1)
148-
add(s2)
149-
var ans []string
150-
for word, n := range counter {
151-
if n == 1 {
152-
ans = append(ans, word)
153-
}
154-
}
155-
return ans
119+
func uncommonFromSentences(s1 string, s2 string) (ans []string) {
120+
cnt := map[string]int{}
121+
for _, s := range strings.Split(s1, " ") {
122+
cnt[s]++
123+
}
124+
for _, s := range strings.Split(s2, " ") {
125+
cnt[s]++
126+
}
127+
for s, v := range cnt {
128+
if v == 1 {
129+
ans = append(ans, s)
130+
}
131+
}
132+
return
133+
}
134+
```
135+
136+
### **TypeScript**
137+
138+
```ts
139+
function uncommonFromSentences(s1: string, s2: string): string[] {
140+
const cnt: Map<string, number> = new Map();
141+
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
142+
cnt.set(s, (cnt.get(s) || 0) + 1);
143+
}
144+
const ans: Array<string> = [];
145+
for (const [s, v] of cnt.entries()) {
146+
if (v == 1) {
147+
ans.push(s);
148+
}
149+
}
150+
return ans;
156151
}
157152
```
158153

@@ -181,6 +176,29 @@ impl Solution {
181176
}
182177
```
183178

179+
### **JavaScript**
180+
181+
```js
182+
/**
183+
* @param {string} s1
184+
* @param {string} s2
185+
* @return {string[]}
186+
*/
187+
var uncommonFromSentences = function (s1, s2) {
188+
const cnt = new Map();
189+
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
190+
cnt.set(s, (cnt.get(s) || 0) + 1);
191+
}
192+
const ans = [];
193+
for (const [s, v] of cnt.entries()) {
194+
if (v == 1) {
195+
ans.push(s);
196+
}
197+
}
198+
return ans;
199+
};
200+
```
201+
184202
### **...**
185203

186204
```

solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md

+70-56
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,30 @@
3737
```python
3838
class Solution:
3939
def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
40-
c = Counter(s1.split()) + Counter(s2.split())
41-
return [w for w, n in c.items() if n == 1]
40+
cnt = Counter(s1.split()) + Counter(s2.split())
41+
return [s for s, v in cnt.items() if v == 1]
4242
```
4343

4444
### **Java**
4545

4646
```java
4747
class Solution {
48-
4948
public String[] uncommonFromSentences(String s1, String s2) {
50-
Map<String, Integer> counter = new HashMap<>();
51-
add(s1, counter);
52-
add(s2, counter);
49+
Map<String, Integer> cnt = new HashMap<>();
50+
for (String s : s1.split(" ")) {
51+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
52+
}
53+
for (String s : s2.split(" ")) {
54+
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
55+
}
5356
List<String> ans = new ArrayList<>();
54-
for (Map.Entry<String, Integer> e : counter.entrySet()) {
57+
for (var e : cnt.entrySet()) {
5558
if (e.getValue() == 1) {
5659
ans.add(e.getKey());
5760
}
5861
}
5962
return ans.toArray(new String[0]);
6063
}
61-
62-
private void add(String s, Map<String, Integer> counter) {
63-
for (String w : s.split(" ")) {
64-
counter.put(w, counter.getOrDefault(w, 0) + 1);
65-
}
66-
}
67-
}
68-
```
69-
70-
### **TypeScript**
71-
72-
```ts
73-
function uncommonFromSentences(s1: string, s2: string): string[] {
74-
let hashMap: Map<string, number> = new Map();
75-
for (let str of [...s1.split(' '), ...s2.split(' ')]) {
76-
hashMap.set(str, (hashMap.get(str) || 0) + 1);
77-
}
78-
let ans: Array<string> = [];
79-
for (let [key, count] of hashMap.entries()) {
80-
if (count == 1) {
81-
ans.push(key);
82-
}
83-
}
84-
return ans;
8564
}
8665
```
8766

@@ -91,20 +70,16 @@ function uncommonFromSentences(s1: string, s2: string): string[] {
9170
class Solution {
9271
public:
9372
vector<string> uncommonFromSentences(string s1, string s2) {
94-
unordered_map<string, int> counter;
95-
96-
auto add = [&](const string& s) {
73+
unordered_map<string, int> cnt;
74+
auto add = [&](string& s) {
9775
stringstream ss(s);
98-
string word;
99-
while (ss >> word) ++counter[move(word)];
76+
string w;
77+
while (ss >> w) ++cnt[move(w)];
10078
};
101-
10279
add(s1);
10380
add(s2);
10481
vector<string> ans;
105-
for (auto& [word, n] : counter)
106-
if (n == 1)
107-
ans.push_back(word);
82+
for (auto& [s, v] : cnt) if (v == 1) ans.emplace_back(s);
10883
return ans;
10984
}
11085
};
@@ -113,22 +88,38 @@ public:
11388
### **Go**
11489
11590
```go
116-
func uncommonFromSentences(s1 string, s2 string) []string {
117-
counter := make(map[string]int)
118-
add := func(s string) {
119-
for _, w := range strings.Split(s, " ") {
120-
counter[w]++
121-
}
122-
}
123-
add(s1)
124-
add(s2)
125-
var ans []string
126-
for word, n := range counter {
127-
if n == 1 {
128-
ans = append(ans, word)
129-
}
130-
}
131-
return ans
91+
func uncommonFromSentences(s1 string, s2 string) (ans []string) {
92+
cnt := map[string]int{}
93+
for _, s := range strings.Split(s1, " ") {
94+
cnt[s]++
95+
}
96+
for _, s := range strings.Split(s2, " ") {
97+
cnt[s]++
98+
}
99+
for s, v := range cnt {
100+
if v == 1 {
101+
ans = append(ans, s)
102+
}
103+
}
104+
return
105+
}
106+
```
107+
108+
### **TypeScript**
109+
110+
```ts
111+
function uncommonFromSentences(s1: string, s2: string): string[] {
112+
const cnt: Map<string, number> = new Map();
113+
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
114+
cnt.set(s, (cnt.get(s) || 0) + 1);
115+
}
116+
const ans: Array<string> = [];
117+
for (const [s, v] of cnt.entries()) {
118+
if (v == 1) {
119+
ans.push(s);
120+
}
121+
}
122+
return ans;
132123
}
133124
```
134125

@@ -157,6 +148,29 @@ impl Solution {
157148
}
158149
```
159150

151+
### **JavaScript**
152+
153+
```js
154+
/**
155+
* @param {string} s1
156+
* @param {string} s2
157+
* @return {string[]}
158+
*/
159+
var uncommonFromSentences = function (s1, s2) {
160+
const cnt = new Map();
161+
for (const s of [...s1.split(' '), ...s2.split(' ')]) {
162+
cnt.set(s, (cnt.get(s) || 0) + 1);
163+
}
164+
const ans = [];
165+
for (const [s, v] of cnt.entries()) {
166+
if (v == 1) {
167+
ans.push(s);
168+
}
169+
}
170+
return ans;
171+
};
172+
```
173+
160174
### **...**
161175

162176
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
class Solution {
22
public:
33
vector<string> uncommonFromSentences(string s1, string s2) {
4-
unordered_map<string, int> counter;
5-
6-
auto add = [&](const string& s) {
4+
unordered_map<string, int> cnt;
5+
auto add = [&](string& s) {
76
stringstream ss(s);
8-
string word;
9-
while (ss >> word) ++counter[move(word)];
7+
string w;
8+
while (ss >> w) ++cnt[move(w)];
109
};
11-
1210
add(s1);
1311
add(s2);
1412
vector<string> ans;
15-
for (auto& [word, n] : counter)
16-
if (n == 1)
17-
ans.push_back(word);
13+
for (auto& [s, v] : cnt) if (v == 1) ans.emplace_back(s);
1814
return ans;
1915
}
2016
};

0 commit comments

Comments
 (0)